Coverage Report

Created: 2026-08-31 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/sspi/Kerberos/kerberos.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Client
3
 * Kerberos Auth Protocol
4
 *
5
 * Copyright 2015 ANSSI, Author Thomas Calderon
6
 * Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
7
 * Copyright 2022 David Fort <contact@hardening-consulting.com>
8
 * Copyright 2022 Isaac Klein <fifthdegree@protonmail.com>
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
#include <winpr/config.h>
23
#include <winpr/library.h>
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <errno.h>
29
#include <fcntl.h>
30
#include <ctype.h>
31
32
#include <winpr/assert.h>
33
#include <winpr/cast.h>
34
#include <winpr/asn1.h>
35
#include <winpr/crt.h>
36
#include <winpr/interlocked.h>
37
#include <winpr/sspi.h>
38
#include <winpr/print.h>
39
#include <winpr/tchar.h>
40
#include <winpr/sysinfo.h>
41
#include <winpr/registry.h>
42
#include <winpr/endian.h>
43
#include <winpr/crypto.h>
44
#include <winpr/path.h>
45
#include <winpr/wtypes.h>
46
#include <winpr/winsock.h>
47
#include <winpr/schannel.h>
48
#include <winpr/secapi.h>
49
50
#include "kerberos.h"
51
52
#ifdef WITH_KRB5_MIT
53
#include "krb5glue.h"
54
#include <profile.h>
55
#endif
56
57
#ifdef WITH_KRB5_HEIMDAL
58
#include "krb5glue.h"
59
#include <krb5-protos.h>
60
#endif
61
62
#include "../sspi.h"
63
#include "../../log.h"
64
65
const SecPkgInfoA KERBEROS_SecPkgInfoA = {
66
  0x000F3BBF,                 /* fCapabilities */
67
  1,                          /* wVersion */
68
  0x0010,                     /* wRPCID */
69
  0x0000BB80,                 /* cbMaxToken : 48k bytes maximum for Windows Server 2012 */
70
  "Kerberos",                 /* Name */
71
  "Kerberos Security Package" /* Comment */
72
};
73
74
static WCHAR KERBEROS_SecPkgInfoW_NameBuffer[32] = WINPR_C_ARRAY_INIT;
75
static WCHAR KERBEROS_SecPkgInfoW_CommentBuffer[32] = WINPR_C_ARRAY_INIT;
76
77
const SecPkgInfoW KERBEROS_SecPkgInfoW = {
78
  0x000F3BBF,                        /* fCapabilities */
79
  1,                                 /* wVersion */
80
  0x0010,                            /* wRPCID */
81
  0x0000BB80,                        /* cbMaxToken : 48k bytes maximum for Windows Server 2012 */
82
  KERBEROS_SecPkgInfoW_NameBuffer,   /* Name */
83
  KERBEROS_SecPkgInfoW_CommentBuffer /* Comment */
84
};
85
86
#ifdef WITH_KRB5
87
0
#define TAG WINPR_TAG("sspi.Kerberos")
88
89
0
#define KRB_TGT_REQ 16
90
0
#define KRB_TGT_REP 17
91
92
enum KERBEROS_STATE
93
{
94
  KERBEROS_STATE_INITIAL,
95
  KERBEROS_STATE_TGT_REQ,
96
  KERBEROS_STATE_TGT_REP,
97
  KERBEROS_STATE_AP_REQ,
98
  KERBEROS_STATE_AP_REP,
99
  KERBEROS_STATE_FINAL
100
};
101
102
typedef struct KRB_CREDENTIALS_st
103
{
104
  volatile LONG refCount;
105
  krb5_context ctx;
106
  char* kdc_url;
107
  krb5_ccache ccache;
108
  krb5_keytab keytab;
109
  krb5_keytab client_keytab;
110
  BOOL own_ccache; /**< Whether we created ccache, and must destroy it after use.  */
111
} KRB_CREDENTIALS;
112
113
struct s_KRB_CONTEXT
114
{
115
  enum KERBEROS_STATE state;
116
  KRB_CREDENTIALS* credentials;
117
  krb5_auth_context auth_ctx;
118
  BOOL acceptor;
119
  uint32_t flags;
120
  uint64_t local_seq;
121
  uint64_t remote_seq;
122
  struct krb5glue_keyset keyset;
123
  BOOL u2u;
124
  char* targetHost;
125
};
126
127
static const WinPrAsn1_OID kerberos_OID = { 9, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
128
static const WinPrAsn1_OID kerberos_u2u_OID = { 10,
129
                                              (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x03" };
130
krb5_error_code kerberos_log_msg(krb5_context ctx, krb5_error_code code, const char* what,
131
                                 const char* file, const char* fkt, size_t line)
132
0
{
133
0
  switch (code)
134
0
  {
135
0
    case 0:
136
0
    case KRB5_KT_END:
137
0
      break;
138
0
    default:
139
0
    {
140
0
      const DWORD level = WLOG_ERROR;
141
142
0
      wLog* log = WLog_Get(TAG);
143
0
      if (WLog_IsLevelActive(log, level))
144
0
      {
145
0
        const char* msg = krb5_get_error_message(ctx, code);
146
0
        WLog_PrintTextMessage(log, level, line, file, fkt, "%s (%s [%d])", what, msg, code);
147
0
        krb5_free_error_message(ctx, msg);
148
0
      }
149
0
    }
150
0
    break;
151
0
  }
152
0
  return code;
153
0
}
154
155
void krb_log_context_encryption(krb5_context ctx, krb5_principal princ)
156
0
{
157
0
#if !defined(WITH_KRB5_HEIMDAL)
158
0
  typedef krb5_error_code KRB5_CALLCONV (*krb5_get_etype_info_fn)(
159
0
      krb5_context context, krb5_principal principal, krb5_get_init_creds_opt* opt,
160
0
      krb5_enctype* enctype_out, krb5_data* salt_out, krb5_data* s2kparams_out);
161
162
0
  krb5_get_etype_info_fn fn =
163
0
      GetProcAddressAs(nullptr, "krb5_get_etype_info", krb5_get_etype_info_fn);
164
165
0
  if (fn)
166
0
  {
167
0
    krb5_get_init_creds_opt opt = WINPR_C_ARRAY_INIT;
168
0
    krb5_enctype enctype = 0;
169
0
    krb5_data salt = WINPR_C_ARRAY_INIT;
170
0
    krb5_data s2kparam = WINPR_C_ARRAY_INIT;
171
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
172
0
    krb5_error_code rv = krb_log_exec(fn, ctx, princ, &opt, &enctype, &salt, &s2kparam);
173
0
    krb5_enctype_to_string(enctype, buffer, sizeof(buffer));
174
0
    const char* msg = krb5_get_error_message(ctx, rv);
175
176
0
    char* saltdata = winpr_BinToHexString(salt.data, salt.length, TRUE);
177
0
    WLog_DBG(TAG, "[%s] enctype=%s, salt[%u]=%s, s2kparam[%u]=%s", msg, buffer, salt.length,
178
0
             saltdata, s2kparam.length, s2kparam.data);
179
180
0
    krb5_free_data_contents(ctx, &salt);
181
0
    krb5_free_data_contents(ctx, &s2kparam);
182
0
    krb5_free_error_message(ctx, msg);
183
0
    free(saltdata);
184
0
  }
185
0
  else
186
0
#endif
187
0
  {
188
0
    WLog_WARN(TAG,
189
0
              "kerberos implementation does not support 'krb5_get_etype_info', not displaying "
190
0
              "encryption information");
191
0
  }
192
0
}
193
194
static void credentials_unref(KRB_CREDENTIALS* credentials);
195
196
static void kerberos_ContextFree(KRB_CONTEXT* ctx, BOOL allocated)
197
0
{
198
0
  if (!ctx)
199
0
    return;
200
201
0
  free(ctx->targetHost);
202
0
  ctx->targetHost = nullptr;
203
204
0
  if (ctx->credentials)
205
0
  {
206
0
    krb5_context krbctx = ctx->credentials->ctx;
207
0
    if (krbctx)
208
0
    {
209
0
      if (ctx->auth_ctx)
210
0
        krb5_auth_con_free(krbctx, ctx->auth_ctx);
211
212
0
      krb5glue_keys_free(krbctx, &ctx->keyset);
213
0
    }
214
215
0
    credentials_unref(ctx->credentials);
216
0
  }
217
218
0
  if (allocated)
219
0
    free(ctx);
220
0
}
221
222
static KRB_CONTEXT* kerberos_ContextNew(KRB_CREDENTIALS* credentials)
223
0
{
224
0
  KRB_CONTEXT* context = nullptr;
225
226
0
  context = (KRB_CONTEXT*)calloc(1, sizeof(KRB_CONTEXT));
227
0
  if (!context)
228
0
    return nullptr;
229
230
0
  context->credentials = credentials;
231
0
  InterlockedIncrement(&credentials->refCount);
232
0
  return context;
233
0
}
234
235
static krb5_error_code krb5_prompter(krb5_context context, void* data,
236
                                     WINPR_ATTR_UNUSED const char* name,
237
                                     WINPR_ATTR_UNUSED const char* banner, int num_prompts,
238
                                     krb5_prompt prompts[])
239
0
{
240
0
  for (int i = 0; i < num_prompts; i++)
241
0
  {
242
0
    krb5_prompt_type type = krb5glue_get_prompt_type(context, prompts, i);
243
0
    if (type && (type == KRB5_PROMPT_TYPE_PREAUTH || type == KRB5_PROMPT_TYPE_PASSWORD) && data)
244
0
    {
245
0
      prompts[i].reply->data = _strdup((const char*)data);
246
247
0
      const size_t len = strlen((const char*)data);
248
0
      if (len > UINT32_MAX)
249
0
        return KRB5KRB_ERR_GENERIC;
250
0
      prompts[i].reply->length = (UINT32)len;
251
0
    }
252
0
  }
253
0
  return 0;
254
0
}
255
256
WINPR_ATTR_NODISCARD static inline krb5glue_key get_key(struct krb5glue_keyset* keyset)
257
0
{
258
0
  return keyset->acceptor_key    ? keyset->acceptor_key
259
0
         : keyset->initiator_key ? keyset->initiator_key
260
0
                                 : keyset->session_key;
261
0
}
262
263
static BOOL isValidIPv4(const char* ipAddress)
264
0
{
265
0
  struct sockaddr_in sa = WINPR_C_ARRAY_INIT;
266
0
  int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
267
0
  return result != 0;
268
0
}
269
270
static BOOL isValidIPv6(const char* ipAddress)
271
0
{
272
0
  struct sockaddr_in6 sa = WINPR_C_ARRAY_INIT;
273
0
  int result = inet_pton(AF_INET6, ipAddress, &(sa.sin6_addr));
274
0
  return result != 0;
275
0
}
276
277
static BOOL isValidIP(const char* ipAddress)
278
0
{
279
0
  return isValidIPv4(ipAddress) || isValidIPv6(ipAddress);
280
0
}
281
282
#if defined(WITH_KRB5_MIT)
283
WINPR_ATTR_MALLOC(free, 1)
284
WINPR_ATTR_NODISCARD
285
static char* get_realm_name(krb5_data realm, size_t* plen)
286
0
{
287
0
  WINPR_ASSERT(plen);
288
0
  *plen = 0;
289
0
  if ((realm.length <= 0) || (!realm.data))
290
0
    return nullptr;
291
292
0
  char* name = nullptr;
293
0
  (void)winpr_asprintf(&name, plen, "krbtgt/%*s@%*s", realm.length, realm.data, realm.length,
294
0
                       realm.data);
295
0
  return name;
296
0
}
297
#elif defined(WITH_KRB5_HEIMDAL)
298
WINPR_ATTR_MALLOC(free, 1)
299
WINPR_ATTR_NODISCARD
300
static char* get_realm_name(Realm realm, size_t* plen)
301
{
302
  WINPR_ASSERT(plen);
303
  *plen = 0;
304
  if (!realm)
305
    return nullptr;
306
307
  char* name = nullptr;
308
  (void)winpr_asprintf(&name, plen, "krbtgt/%s@%s", realm, realm);
309
  return name;
310
}
311
#endif
312
313
static int build_krbtgt(krb5_context ctx, krb5_principal principal, krb5_principal* ptarget)
314
0
{
315
  /* "krbtgt/" + realm + "@" + realm */
316
0
  size_t len = 0;
317
0
  krb5_error_code rv = KRB5_CC_NOMEM;
318
319
0
  char* name = get_realm_name(principal->realm, &len);
320
0
  if (!name || (len == 0))
321
0
    goto fail;
322
323
0
  {
324
0
    krb5_principal target = WINPR_C_ARRAY_INIT;
325
0
    rv = krb5_parse_name(ctx, name, &target);
326
0
    *ptarget = target;
327
0
  }
328
0
fail:
329
0
  free(name);
330
0
  return rv;
331
0
}
332
333
#endif /* WITH_KRB5 */
334
335
static SECURITY_STATUS SEC_ENTRY kerberos_AcquireCredentialsHandleA(
336
    WINPR_ATTR_UNUSED SEC_CHAR* pszPrincipal, WINPR_ATTR_UNUSED SEC_CHAR* pszPackage,
337
    WINPR_ATTR_UNUSED ULONG fCredentialUse, WINPR_ATTR_UNUSED void* pvLogonID,
338
    WINPR_ATTR_UNUSED void* pAuthData, WINPR_ATTR_UNUSED SEC_GET_KEY_FN pGetKeyFn,
339
    WINPR_ATTR_UNUSED void* pvGetKeyArgument, WINPR_ATTR_UNUSED PCredHandle phCredential,
340
    WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
341
0
{
342
0
#ifdef WITH_KRB5
343
0
#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
344
0
  SEC_WINPR_KERBEROS_SETTINGS_V2 krb_settings_v1_buffer = WINPR_C_ARRAY_INIT;
345
0
#endif
346
347
0
  SEC_WINPR_KERBEROS_SETTINGS_V2* krb_settings = nullptr;
348
0
  KRB_CREDENTIALS* credentials = nullptr;
349
0
  krb5_context ctx = nullptr;
350
0
  krb5_ccache ccache = nullptr;
351
0
  krb5_keytab keytab = nullptr;
352
0
  krb5_principal principal = nullptr;
353
0
  char* domain = nullptr;
354
0
  char* username = nullptr;
355
0
  char* password = nullptr;
356
0
  BOOL own_ccache = FALSE;
357
0
  const char* const default_ccache_type = "MEMORY";
358
359
0
  if (pAuthData)
360
0
  {
361
0
    UINT32 identityFlags = sspi_GetAuthIdentityFlags(pAuthData);
362
363
0
#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
364
0
    if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED)
365
0
    {
366
0
      SEC_WINPR_KERBEROS_SETTINGS* krb_settingsV1 =
367
0
          (((SEC_WINNT_AUTH_IDENTITY_WINPR*)pAuthData)->kerberosSettings);
368
0
      if (krb_settingsV1)
369
0
      {
370
0
        krb_settings_v1_buffer.kdcUrl = krb_settingsV1->kdcUrl;
371
0
        krb_settings_v1_buffer.keytab = krb_settingsV1->keytab;
372
0
        krb_settings_v1_buffer.cache = krb_settingsV1->cache;
373
0
        krb_settings_v1_buffer.armorCache = krb_settingsV1->armorCache;
374
0
        krb_settings_v1_buffer.pkinitX509Anchors = krb_settingsV1->pkinitX509Anchors;
375
0
        krb_settings_v1_buffer.pkinitX509Identity = krb_settingsV1->pkinitX509Identity;
376
0
        krb_settings_v1_buffer.withPac = krb_settingsV1->withPac;
377
0
        krb_settings_v1_buffer.startTime = krb_settingsV1->startTime;
378
0
        krb_settings_v1_buffer.renewLifeTime = krb_settingsV1->renewLifeTime;
379
0
        krb_settings_v1_buffer.lifeTime = krb_settingsV1->lifeTime;
380
0
        memcpy(krb_settings_v1_buffer.certSha1, krb_settingsV1->certSha1,
381
0
               sizeof(krb_settings_v1_buffer.certSha1));
382
0
        krb_settings = &krb_settings_v1_buffer;
383
0
      }
384
0
    }
385
0
#endif
386
0
    if (identityFlags & SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2)
387
0
    {
388
0
      const SEC_WINNT_AUTH_IDENTITY_WINPR_V2* auth =
389
0
          (const SEC_WINNT_AUTH_IDENTITY_WINPR_V2*)pAuthData;
390
0
      WINPR_ASSERT(auth);
391
0
      if (auth->version < SEC_WINNT_AUTH_IDENTITY_WINPR_V2_REVISION_1)
392
0
        goto cleanup;
393
0
      krb_settings = auth->kerberosSettingsV2;
394
0
    }
395
396
0
    if (!sspi_CopyAuthIdentityFieldsA((const SEC_WINNT_AUTH_IDENTITY_INFO*)pAuthData, &username,
397
0
                                      &domain, &password))
398
0
    {
399
0
      WLog_ERR(TAG, "Failed to copy auth identity fields");
400
0
      goto cleanup;
401
0
    }
402
403
0
    if (!pszPrincipal)
404
0
      pszPrincipal = username;
405
0
  }
406
407
0
  if (krb_log_exec_ptr(krb5_init_context, &ctx))
408
0
    goto cleanup;
409
410
0
  if (domain)
411
0
  {
412
0
    char* udomain = _strdup(domain);
413
0
    if (!udomain)
414
0
      goto cleanup;
415
416
0
    CharUpperA(udomain);
417
    /* Will use domain if realm is not specified in username */
418
0
    krb5_error_code rv = krb_log_exec(krb5_set_default_realm, ctx, udomain);
419
0
    free(udomain);
420
421
0
    if (rv)
422
0
      goto cleanup;
423
0
  }
424
425
0
  if (pszPrincipal)
426
0
  {
427
0
    char* cpszPrincipal = _strdup(pszPrincipal);
428
0
    if (!cpszPrincipal)
429
0
      goto cleanup;
430
431
    /* Find realm component if included and convert to uppercase */
432
0
    char* p = strchr(cpszPrincipal, '@');
433
0
    if (p)
434
0
      CharUpperA(p);
435
436
0
    krb5_error_code rv = krb_log_exec(krb5_parse_name, ctx, cpszPrincipal, &principal);
437
0
    free(cpszPrincipal);
438
439
0
    if (rv)
440
0
      goto cleanup;
441
0
    WINPR_ASSERT(principal);
442
0
  }
443
444
0
  if (krb_settings && krb_settings->cache)
445
0
  {
446
0
    if ((krb_log_exec(krb5_cc_set_default_name, ctx, krb_settings->cache)))
447
0
      goto cleanup;
448
0
  }
449
0
  else
450
0
    own_ccache = TRUE;
451
452
0
  if (principal)
453
0
  {
454
    /* Use the default cache if it's initialized with the right principal */
455
0
    if (krb5_cc_cache_match(ctx, principal, &ccache) == KRB5_CC_NOTFOUND)
456
0
    {
457
0
      if (own_ccache)
458
0
      {
459
0
        if (krb_log_exec(krb5_cc_new_unique, ctx, default_ccache_type, nullptr, &ccache))
460
0
          goto cleanup;
461
0
      }
462
0
      else
463
0
      {
464
0
        if (krb_log_exec(krb5_cc_resolve, ctx, krb_settings->cache, &ccache))
465
0
          goto cleanup;
466
0
      }
467
468
0
      if (krb_log_exec(krb5_cc_initialize, ctx, ccache, principal))
469
0
        goto cleanup;
470
0
    }
471
0
    else
472
0
    {
473
0
      if (krb_log_exec(krb5_cc_default, ctx, &ccache))
474
0
        goto cleanup;
475
0
      own_ccache = FALSE;
476
0
    }
477
0
    WINPR_ASSERT(ccache);
478
0
  }
479
0
  else if (fCredentialUse & SECPKG_CRED_OUTBOUND)
480
0
  {
481
    /* Use the default cache with it's default principal */
482
0
    if (krb_log_exec(krb5_cc_default, ctx, &ccache))
483
0
      goto cleanup;
484
0
    if (krb_log_exec(krb5_cc_get_principal, ctx, ccache, &principal))
485
0
      goto cleanup;
486
0
    WINPR_ASSERT(ccache);
487
0
    own_ccache = FALSE;
488
0
  }
489
0
  else
490
0
  {
491
0
    if (own_ccache)
492
0
    {
493
0
      if (krb_log_exec(krb5_cc_new_unique, ctx, default_ccache_type, nullptr, &ccache))
494
0
        goto cleanup;
495
0
    }
496
0
    else
497
0
    {
498
0
      if (krb_log_exec(krb5_cc_resolve, ctx, krb_settings->cache, &ccache))
499
0
        goto cleanup;
500
0
    }
501
0
    WINPR_ASSERT(ccache);
502
0
  }
503
504
0
  if (krb_settings && krb_settings->keytab)
505
0
  {
506
0
    if (krb_log_exec(krb5_kt_resolve, ctx, krb_settings->keytab, &keytab))
507
0
      goto cleanup;
508
0
  }
509
0
  else
510
0
  {
511
0
    if (fCredentialUse & SECPKG_CRED_INBOUND)
512
0
      if (krb_log_exec(krb5_kt_default, ctx, &keytab))
513
0
        goto cleanup;
514
0
  }
515
516
  /* Get initial credentials if required */
517
0
  if (fCredentialUse & SECPKG_CRED_OUTBOUND)
518
0
  {
519
0
    krb5_creds creds = WINPR_C_ARRAY_INIT;
520
0
    krb5_creds matchCreds = WINPR_C_ARRAY_INIT;
521
0
    krb5_flags matchFlags = KRB5_TC_MATCH_TIMES;
522
523
0
    krb5_timeofday(ctx, &matchCreds.times.endtime);
524
0
    matchCreds.times.endtime += 60;
525
0
    matchCreds.client = principal;
526
527
0
    WINPR_ASSERT(principal);
528
0
    WINPR_ASSERT(ctx);
529
0
    WINPR_ASSERT(ccache);
530
0
    if (krb_log_exec(build_krbtgt, ctx, principal, &matchCreds.server))
531
0
      goto cleanup;
532
533
0
    int rv = krb5_cc_retrieve_cred(ctx, ccache, matchFlags, &matchCreds, &creds);
534
0
    krb5_free_principal(ctx, matchCreds.server);
535
0
    krb5_free_cred_contents(ctx, &creds);
536
0
    if (rv)
537
0
    {
538
0
      if (krb_log_exec(krb5glue_get_init_creds, ctx, principal, ccache, krb5_prompter,
539
0
                       password, krb_settings))
540
0
        goto cleanup;
541
0
    }
542
0
  }
543
544
0
  credentials = calloc(1, sizeof(KRB_CREDENTIALS));
545
0
  if (!credentials)
546
0
    goto cleanup;
547
0
  credentials->refCount = 1;
548
0
  credentials->ctx = ctx;
549
0
  credentials->ccache = ccache;
550
0
  credentials->keytab = keytab;
551
0
  credentials->own_ccache = own_ccache;
552
553
0
cleanup:
554
555
0
  free(domain);
556
0
  free(username);
557
0
  free(password);
558
559
0
  if (principal)
560
0
    krb5_free_principal(ctx, principal);
561
0
  if (ctx)
562
0
  {
563
0
    if (!credentials)
564
0
    {
565
0
      if (ccache)
566
0
      {
567
0
        if (own_ccache)
568
0
          krb5_cc_destroy(ctx, ccache);
569
0
        else
570
0
          krb5_cc_close(ctx, ccache);
571
0
      }
572
0
      if (keytab)
573
0
        krb5_kt_close(ctx, keytab);
574
575
0
      krb5_free_context(ctx);
576
0
    }
577
0
  }
578
579
  /* If we managed to get credentials set the output */
580
0
  if (credentials)
581
0
  {
582
0
    sspi_SecureHandleSetLowerPointer(phCredential, (void*)credentials);
583
0
    sspi_SecureHandleSetUpperPointer(phCredential, (void*)KERBEROS_SSP_NAME);
584
0
    return SEC_E_OK;
585
0
  }
586
587
0
  return SEC_E_NO_CREDENTIALS;
588
#else
589
  return SEC_E_UNSUPPORTED_FUNCTION;
590
#endif
591
0
}
592
593
static SECURITY_STATUS SEC_ENTRY kerberos_AcquireCredentialsHandleW(
594
    SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
595
    void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
596
    PTimeStamp ptsExpiry)
597
0
{
598
0
  SECURITY_STATUS status = SEC_E_INSUFFICIENT_MEMORY;
599
0
  char* principal = nullptr;
600
0
  char* package = nullptr;
601
602
0
  if (pszPrincipal)
603
0
  {
604
0
    principal = ConvertWCharToUtf8Alloc(pszPrincipal, nullptr);
605
0
    if (!principal)
606
0
      goto fail;
607
0
  }
608
0
  if (pszPackage)
609
0
  {
610
0
    package = ConvertWCharToUtf8Alloc(pszPackage, nullptr);
611
0
    if (!package)
612
0
      goto fail;
613
0
  }
614
615
0
  status =
616
0
      kerberos_AcquireCredentialsHandleA(principal, package, fCredentialUse, pvLogonID, pAuthData,
617
0
                                         pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry);
618
619
0
fail:
620
0
  free(principal);
621
0
  free(package);
622
623
0
  return status;
624
0
}
625
626
#ifdef WITH_KRB5
627
static void credentials_unref(KRB_CREDENTIALS* credentials)
628
0
{
629
0
  WINPR_ASSERT(credentials);
630
631
0
  if (InterlockedDecrement(&credentials->refCount))
632
0
    return;
633
634
0
  free(credentials->kdc_url);
635
636
0
  if (credentials->ccache)
637
0
  {
638
0
    if (credentials->own_ccache)
639
0
      krb5_cc_destroy(credentials->ctx, credentials->ccache);
640
0
    else
641
0
      krb5_cc_close(credentials->ctx, credentials->ccache);
642
0
  }
643
0
  if (credentials->keytab)
644
0
    krb5_kt_close(credentials->ctx, credentials->keytab);
645
646
0
  krb5_free_context(credentials->ctx);
647
0
  free(credentials);
648
0
}
649
#endif
650
651
static SECURITY_STATUS
652
    SEC_ENTRY kerberos_FreeCredentialsHandle(WINPR_ATTR_UNUSED PCredHandle phCredential)
653
0
{
654
0
#ifdef WITH_KRB5
655
0
  KRB_CREDENTIALS* credentials = sspi_SecureHandleGetLowerPointer(phCredential);
656
0
  sspi_SecureHandleInvalidate(phCredential);
657
0
  if (!credentials)
658
0
    return SEC_E_INVALID_HANDLE;
659
660
0
  credentials_unref(credentials);
661
662
0
  return SEC_E_OK;
663
#else
664
  return SEC_E_UNSUPPORTED_FUNCTION;
665
#endif
666
0
}
667
668
static SECURITY_STATUS SEC_ENTRY kerberos_QueryCredentialsAttributesW(
669
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
670
    WINPR_ATTR_UNUSED void* pBuffer)
671
0
{
672
0
#ifdef WITH_KRB5
673
0
  switch (ulAttribute)
674
0
  {
675
0
    case SECPKG_CRED_ATTR_NAMES:
676
0
      return SEC_E_OK;
677
0
    default:
678
0
      WLog_ERR(TAG, "TODO: QueryCredentialsAttributesW, implement ulAttribute=%08" PRIx32,
679
0
               ulAttribute);
680
0
      return SEC_E_UNSUPPORTED_FUNCTION;
681
0
  }
682
683
#else
684
  return SEC_E_UNSUPPORTED_FUNCTION;
685
#endif
686
0
}
687
688
static SECURITY_STATUS SEC_ENTRY kerberos_QueryCredentialsAttributesA(PCredHandle phCredential,
689
                                                                      ULONG ulAttribute,
690
                                                                      void* pBuffer)
691
0
{
692
0
  return kerberos_QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
693
0
}
694
695
#ifdef WITH_KRB5
696
697
static BOOL kerberos_mk_tgt_token(SecBuffer* buf, int msg_type, char* sname, char* host,
698
                                  const krb5_data* ticket)
699
0
{
700
0
  WinPrAsn1Encoder* enc = nullptr;
701
0
  WinPrAsn1_MemoryChunk data;
702
0
  wStream s;
703
0
  size_t len = 0;
704
0
  sspi_gss_data token;
705
0
  BOOL ret = FALSE;
706
707
0
  WINPR_ASSERT(buf);
708
709
0
  if (msg_type != KRB_TGT_REQ && msg_type != KRB_TGT_REP)
710
0
    return FALSE;
711
0
  if (msg_type == KRB_TGT_REP && !ticket)
712
0
    return FALSE;
713
714
0
  enc = WinPrAsn1Encoder_New(WINPR_ASN1_DER);
715
0
  if (!enc)
716
0
    return FALSE;
717
718
  /* KERB-TGT-REQUEST (SEQUENCE) */
719
0
  if (!WinPrAsn1EncSeqContainer(enc))
720
0
    goto cleanup;
721
722
  /* pvno [0] INTEGER */
723
0
  if (!WinPrAsn1EncContextualInteger(enc, 0, 5))
724
0
    goto cleanup;
725
726
  /* msg-type [1] INTEGER */
727
0
  if (!WinPrAsn1EncContextualInteger(enc, 1, msg_type))
728
0
    goto cleanup;
729
730
0
  if (msg_type == KRB_TGT_REQ && sname)
731
0
  {
732
    /* server-name [2] PrincipalName (SEQUENCE) */
733
0
    if (!WinPrAsn1EncContextualSeqContainer(enc, 2))
734
0
      goto cleanup;
735
736
    /* name-type [0] INTEGER */
737
0
    if (!WinPrAsn1EncContextualInteger(enc, 0, KRB5_NT_SRV_HST))
738
0
      goto cleanup;
739
740
    /* name-string [1] SEQUENCE OF GeneralString */
741
0
    if (!WinPrAsn1EncContextualSeqContainer(enc, 1))
742
0
      goto cleanup;
743
744
0
    if (!WinPrAsn1EncGeneralString(enc, sname))
745
0
      goto cleanup;
746
747
0
    if (host && !WinPrAsn1EncGeneralString(enc, host))
748
0
      goto cleanup;
749
750
0
    if (!WinPrAsn1EncEndContainer(enc) || !WinPrAsn1EncEndContainer(enc))
751
0
      goto cleanup;
752
0
  }
753
0
  else if (msg_type == KRB_TGT_REP)
754
0
  {
755
    /* ticket [2] Ticket */
756
0
    data.data = (BYTE*)ticket->data;
757
0
    data.len = ticket->length;
758
0
    if (!WinPrAsn1EncContextualRawContent(enc, 2, &data))
759
0
      goto cleanup;
760
0
  }
761
762
0
  if (!WinPrAsn1EncEndContainer(enc))
763
0
    goto cleanup;
764
765
0
  if (!WinPrAsn1EncStreamSize(enc, &len) || len > buf->cbBuffer)
766
0
    goto cleanup;
767
768
0
  Stream_StaticInit(&s, buf->pvBuffer, len);
769
0
  if (!WinPrAsn1EncToStream(enc, &s))
770
0
    goto cleanup;
771
772
0
  token.data = buf->pvBuffer;
773
0
  token.length = (UINT)len;
774
0
  if (sspi_gss_wrap_token(buf, &kerberos_u2u_OID,
775
0
                          msg_type == KRB_TGT_REQ ? TOK_ID_TGT_REQ : TOK_ID_TGT_REP, &token))
776
0
    ret = TRUE;
777
778
0
cleanup:
779
0
  WinPrAsn1Encoder_Free(&enc);
780
0
  return ret;
781
0
}
782
783
static BOOL append(char* dst, size_t dstSize, const char* src)
784
0
{
785
0
  const size_t dlen = strnlen(dst, dstSize);
786
0
  const size_t slen = strlen(src);
787
0
  if (dlen + slen >= dstSize)
788
0
    return FALSE;
789
0
  if (!strncat(dst, src, dstSize - dlen))
790
0
    return FALSE;
791
0
  return TRUE;
792
0
}
793
794
static BOOL kerberos_rd_tgt_req_tag2(WinPrAsn1Decoder* dec, char* buf, size_t len)
795
0
{
796
0
  BOOL rc = FALSE;
797
0
  WinPrAsn1Decoder seq = WinPrAsn1Decoder_init();
798
799
  /* server-name [2] PrincipalName (SEQUENCE) */
800
0
  if (!WinPrAsn1DecReadSequence(dec, &seq))
801
0
    goto end;
802
803
  /* name-type [0] INTEGER */
804
0
  {
805
0
    BOOL error = FALSE;
806
0
    {
807
0
      WinPrAsn1_INTEGER val = 0;
808
0
      if (!WinPrAsn1DecReadContextualInteger(&seq, 0, &error, &val))
809
0
        goto end;
810
0
    }
811
812
    /* name-string [1] SEQUENCE OF GeneralString */
813
0
    if (!WinPrAsn1DecReadContextualSequence(&seq, 1, &error, dec))
814
0
      goto end;
815
0
  }
816
817
0
  {
818
0
    WinPrAsn1_tag tag = 0;
819
0
    BOOL first = TRUE;
820
0
    while (WinPrAsn1DecPeekTag(dec, &tag))
821
0
    {
822
0
      BOOL success = FALSE;
823
0
      char* lstr = nullptr;
824
0
      if (!WinPrAsn1DecReadGeneralString(dec, &lstr))
825
0
        goto fail;
826
827
0
      if (!first)
828
0
      {
829
0
        if (!append(buf, len, "/"))
830
0
          goto fail;
831
0
      }
832
0
      first = FALSE;
833
834
0
      if (!append(buf, len, lstr))
835
0
        goto fail;
836
837
0
      success = TRUE;
838
0
    fail:
839
0
      free(lstr);
840
0
      if (!success)
841
0
        goto end;
842
0
    }
843
0
  }
844
845
0
  rc = TRUE;
846
0
end:
847
0
  return rc;
848
0
}
849
850
static BOOL kerberos_rd_tgt_req_tag3(WinPrAsn1Decoder* dec, char* buf, size_t len)
851
0
{
852
  /* realm [3] Realm */
853
0
  BOOL rc = FALSE;
854
0
  WinPrAsn1_STRING str = nullptr;
855
0
  if (!WinPrAsn1DecReadGeneralString(dec, &str))
856
0
    goto end;
857
858
0
  if (!append(buf, len, "@"))
859
0
    goto end;
860
0
  if (!append(buf, len, str))
861
0
    goto end;
862
863
0
  rc = TRUE;
864
0
end:
865
0
  free(str);
866
0
  return rc;
867
0
}
868
869
static BOOL kerberos_rd_tgt_req(WinPrAsn1Decoder* dec, char** target)
870
0
{
871
0
  BOOL rc = FALSE;
872
873
0
  if (!target)
874
0
    return FALSE;
875
0
  *target = nullptr;
876
877
0
  wStream s = WinPrAsn1DecGetStream(dec);
878
0
  const size_t len = Stream_Length(&s);
879
0
  if (len == 0)
880
0
    return TRUE;
881
882
0
  WinPrAsn1Decoder dec2 = WinPrAsn1Decoder_init();
883
0
  WinPrAsn1_tagId tag = 0;
884
0
  if (WinPrAsn1DecReadContextualTag(dec, &tag, &dec2) == 0)
885
0
    return FALSE;
886
887
0
  char* buf = calloc(len + 1, sizeof(char));
888
0
  if (!buf)
889
0
    return FALSE;
890
891
  /* We expect ASN1 context tag values 2 or 3.
892
   *
893
   * In case we got value 2 an (optional) context tag value 3 might follow.
894
   */
895
0
  BOOL checkForTag3 = TRUE;
896
0
  if (tag == 2)
897
0
  {
898
0
    rc = kerberos_rd_tgt_req_tag2(&dec2, buf, len);
899
0
    if (rc)
900
0
    {
901
0
      const size_t res = WinPrAsn1DecReadContextualTag(dec, &tag, dec);
902
0
      if (res == 0)
903
0
        checkForTag3 = FALSE;
904
0
    }
905
0
  }
906
907
0
  if (checkForTag3)
908
0
  {
909
0
    if (tag == 3)
910
0
      rc = kerberos_rd_tgt_req_tag3(&dec2, buf, len);
911
0
    else
912
0
      rc = FALSE;
913
0
  }
914
915
0
  if (rc)
916
0
    *target = buf;
917
0
  else
918
0
    free(buf);
919
0
  return rc;
920
0
}
921
922
static BOOL kerberos_rd_tgt_rep(WinPrAsn1Decoder* dec, krb5_data* ticket)
923
0
{
924
0
  if (!ticket)
925
0
    return FALSE;
926
927
  /* ticket [2] Ticket */
928
0
  WinPrAsn1Decoder asnTicket = WinPrAsn1Decoder_init();
929
0
  WinPrAsn1_tagId tag = 0;
930
0
  if (WinPrAsn1DecReadContextualTag(dec, &tag, &asnTicket) == 0)
931
0
    return FALSE;
932
933
0
  if (tag != 2)
934
0
    return FALSE;
935
936
0
  wStream s = WinPrAsn1DecGetStream(&asnTicket);
937
0
  ticket->data = Stream_BufferAs(&s, char);
938
939
0
  const size_t len = Stream_Length(&s);
940
0
  if (len > UINT32_MAX)
941
0
    return FALSE;
942
0
  ticket->length = (UINT32)len;
943
0
  return TRUE;
944
0
}
945
946
static BOOL kerberos_rd_tgt_token(const sspi_gss_data* token, char** target, krb5_data* ticket)
947
0
{
948
0
  BOOL error = 0;
949
0
  WinPrAsn1_INTEGER val = 0;
950
951
0
  WINPR_ASSERT(token);
952
953
0
  if (target)
954
0
    *target = nullptr;
955
956
0
  WinPrAsn1Decoder der = WinPrAsn1Decoder_init();
957
0
  WinPrAsn1Decoder_InitMem(&der, WINPR_ASN1_DER, (BYTE*)token->data, token->length);
958
959
  /* KERB-TGT-REQUEST (SEQUENCE) */
960
0
  WinPrAsn1Decoder seq = WinPrAsn1Decoder_init();
961
0
  if (!WinPrAsn1DecReadSequence(&der, &seq))
962
0
    return FALSE;
963
964
  /* pvno [0] INTEGER */
965
0
  if (!WinPrAsn1DecReadContextualInteger(&seq, 0, &error, &val) || val != 5)
966
0
    return FALSE;
967
968
  /* msg-type [1] INTEGER */
969
0
  if (!WinPrAsn1DecReadContextualInteger(&seq, 1, &error, &val))
970
0
    return FALSE;
971
972
0
  switch (val)
973
0
  {
974
0
    case KRB_TGT_REQ:
975
0
      return kerberos_rd_tgt_req(&seq, target);
976
0
    case KRB_TGT_REP:
977
0
      return kerberos_rd_tgt_rep(&seq, ticket);
978
0
    default:
979
0
      break;
980
0
  }
981
0
  return FALSE;
982
0
}
983
984
static BOOL kerberos_hash_channel_bindings(WINPR_DIGEST_CTX* md5, SEC_CHANNEL_BINDINGS* bindings)
985
0
{
986
0
  BYTE buf[4];
987
988
0
  winpr_Data_Write_UINT32(buf, bindings->dwInitiatorAddrType);
989
0
  if (!winpr_Digest_Update(md5, buf, 4))
990
0
    return FALSE;
991
992
0
  winpr_Data_Write_UINT32(buf, bindings->cbInitiatorLength);
993
0
  if (!winpr_Digest_Update(md5, buf, 4))
994
0
    return FALSE;
995
996
0
  if (bindings->cbInitiatorLength &&
997
0
      !winpr_Digest_Update(md5, (BYTE*)bindings + bindings->dwInitiatorOffset,
998
0
                           bindings->cbInitiatorLength))
999
0
    return FALSE;
1000
1001
0
  winpr_Data_Write_UINT32(buf, bindings->dwAcceptorAddrType);
1002
0
  if (!winpr_Digest_Update(md5, buf, 4))
1003
0
    return FALSE;
1004
1005
0
  winpr_Data_Write_UINT32(buf, bindings->cbAcceptorLength);
1006
0
  if (!winpr_Digest_Update(md5, buf, 4))
1007
0
    return FALSE;
1008
1009
0
  if (bindings->cbAcceptorLength &&
1010
0
      !winpr_Digest_Update(md5, (BYTE*)bindings + bindings->dwAcceptorOffset,
1011
0
                           bindings->cbAcceptorLength))
1012
0
    return FALSE;
1013
1014
0
  winpr_Data_Write_UINT32(buf, bindings->cbApplicationDataLength);
1015
0
  if (!winpr_Digest_Update(md5, buf, 4))
1016
0
    return FALSE;
1017
1018
0
  if (bindings->cbApplicationDataLength &&
1019
0
      !winpr_Digest_Update(md5, (BYTE*)bindings + bindings->dwApplicationDataOffset,
1020
0
                           bindings->cbApplicationDataLength))
1021
0
    return FALSE;
1022
1023
0
  return TRUE;
1024
0
}
1025
1026
#endif /* WITH_KRB5 */
1027
1028
static SECURITY_STATUS SEC_ENTRY kerberos_InitializeSecurityContextA(
1029
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED PCtxtHandle phContext,
1030
    WINPR_ATTR_UNUSED SEC_CHAR* pszTargetName, WINPR_ATTR_UNUSED ULONG fContextReq,
1031
    WINPR_ATTR_UNUSED ULONG Reserved1, WINPR_ATTR_UNUSED ULONG TargetDataRep,
1032
    WINPR_ATTR_UNUSED PSecBufferDesc pInput, WINPR_ATTR_UNUSED ULONG Reserved2,
1033
    WINPR_ATTR_UNUSED PCtxtHandle phNewContext, WINPR_ATTR_UNUSED PSecBufferDesc pOutput,
1034
    WINPR_ATTR_UNUSED ULONG* pfContextAttr, WINPR_ATTR_UNUSED PTimeStamp ptsExpiry)
1035
0
{
1036
0
#ifdef WITH_KRB5
1037
0
  PSecBuffer input_buffer = nullptr;
1038
0
  PSecBuffer output_buffer = nullptr;
1039
0
  PSecBuffer bindings_buffer = nullptr;
1040
0
  WINPR_DIGEST_CTX* md5 = nullptr;
1041
0
  char* target = nullptr;
1042
0
  char* sname = nullptr;
1043
0
  char* host = nullptr;
1044
0
  krb5_data input_token = WINPR_C_ARRAY_INIT;
1045
0
  krb5_data output_token = WINPR_C_ARRAY_INIT;
1046
0
  SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
1047
0
  WinPrAsn1_OID oid = WINPR_C_ARRAY_INIT;
1048
0
  uint16_t tok_id = 0;
1049
0
  krb5_ap_rep_enc_part* reply = nullptr;
1050
0
  krb5_flags ap_flags = AP_OPTS_USE_SUBKEY;
1051
0
  char cksum_contents[24] = WINPR_C_ARRAY_INIT;
1052
0
  krb5_data cksum = WINPR_C_ARRAY_INIT;
1053
0
  krb5_creds in_creds = WINPR_C_ARRAY_INIT;
1054
0
  krb5_creds* creds = nullptr;
1055
0
  BOOL isNewContext = FALSE;
1056
0
  KRB_CONTEXT* context = nullptr;
1057
0
  KRB_CREDENTIALS* credentials = sspi_SecureHandleGetLowerPointer(phCredential);
1058
1059
  /* behave like windows SSPIs that don't want empty context */
1060
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
1061
0
    return SEC_E_INVALID_HANDLE;
1062
1063
0
  context = sspi_SecureHandleGetLowerPointer(phContext);
1064
1065
0
  if (!credentials)
1066
0
    return SEC_E_NO_CREDENTIALS;
1067
1068
0
  if (pInput)
1069
0
  {
1070
0
    input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
1071
0
    bindings_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_CHANNEL_BINDINGS);
1072
0
  }
1073
0
  if (pOutput)
1074
0
    output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
1075
1076
0
  if (fContextReq & ISC_REQ_MUTUAL_AUTH)
1077
0
    ap_flags |= AP_OPTS_MUTUAL_REQUIRED;
1078
1079
0
  if (fContextReq & ISC_REQ_USE_SESSION_KEY)
1080
0
    ap_flags |= AP_OPTS_USE_SESSION_KEY;
1081
1082
  /* Split target name into service/hostname components */
1083
0
  if (pszTargetName)
1084
0
  {
1085
0
    target = _strdup(pszTargetName);
1086
0
    if (!target)
1087
0
    {
1088
0
      status = SEC_E_INSUFFICIENT_MEMORY;
1089
0
      goto cleanup;
1090
0
    }
1091
0
    host = strchr(target, '/');
1092
0
    if (host)
1093
0
    {
1094
0
      *host++ = 0;
1095
0
      sname = target;
1096
0
    }
1097
0
    else
1098
0
      host = target;
1099
0
    if (isValidIP(host))
1100
0
    {
1101
0
      status = SEC_E_NO_CREDENTIALS;
1102
0
      goto cleanup;
1103
0
    }
1104
0
  }
1105
1106
0
  if (!context)
1107
0
  {
1108
0
    context = kerberos_ContextNew(credentials);
1109
0
    if (!context)
1110
0
    {
1111
0
      status = SEC_E_INSUFFICIENT_MEMORY;
1112
0
      goto cleanup;
1113
0
    }
1114
1115
0
    isNewContext = TRUE;
1116
1117
0
    if (host)
1118
0
      context->targetHost = _strdup(host);
1119
0
    if (!context->targetHost)
1120
0
    {
1121
0
      status = SEC_E_INSUFFICIENT_MEMORY;
1122
0
      goto cleanup;
1123
0
    }
1124
1125
0
    if (fContextReq & ISC_REQ_USE_SESSION_KEY)
1126
0
    {
1127
0
      context->state = KERBEROS_STATE_TGT_REQ;
1128
0
      context->u2u = TRUE;
1129
0
    }
1130
0
    else
1131
0
      context->state = KERBEROS_STATE_AP_REQ;
1132
0
  }
1133
0
  else
1134
0
  {
1135
0
    if (!input_buffer || !sspi_gss_unwrap_token(input_buffer, &oid, &tok_id, &input_token))
1136
0
      goto bad_token;
1137
0
    if ((context->u2u && !sspi_gss_oid_compare(&oid, &kerberos_u2u_OID)) ||
1138
0
        (!context->u2u && !sspi_gss_oid_compare(&oid, &kerberos_OID)))
1139
0
      goto bad_token;
1140
0
  }
1141
1142
  /* SSPI flags are compatible with GSS flags except INTEG_FLAG */
1143
0
  context->flags |= (fContextReq & 0x1F);
1144
0
  if ((fContextReq & ISC_REQ_INTEGRITY) && !(fContextReq & ISC_REQ_NO_INTEGRITY))
1145
0
    context->flags |= SSPI_GSS_C_INTEG_FLAG;
1146
1147
0
  switch (context->state)
1148
0
  {
1149
0
    case KERBEROS_STATE_TGT_REQ:
1150
1151
0
      if (!kerberos_mk_tgt_token(output_buffer, KRB_TGT_REQ, sname, host, nullptr))
1152
0
        goto cleanup;
1153
1154
0
      context->state = KERBEROS_STATE_TGT_REP;
1155
0
      status = SEC_I_CONTINUE_NEEDED;
1156
0
      break;
1157
1158
0
    case KERBEROS_STATE_TGT_REP:
1159
1160
0
      if (tok_id != TOK_ID_TGT_REP)
1161
0
        goto bad_token;
1162
1163
0
      if (!kerberos_rd_tgt_token(&input_token, nullptr, &in_creds.second_ticket))
1164
0
        goto bad_token;
1165
1166
      /* Continue to AP-REQ */
1167
      /* fallthrough */
1168
0
      WINPR_FALLTHROUGH
1169
1170
0
    case KERBEROS_STATE_AP_REQ:
1171
1172
      /* Set auth_context options */
1173
0
      if (krb_log_exec(krb5_auth_con_init, credentials->ctx, &context->auth_ctx))
1174
0
        goto cleanup;
1175
0
      if (krb_log_exec(krb5_auth_con_setflags, credentials->ctx, context->auth_ctx,
1176
0
                       KRB5_AUTH_CONTEXT_DO_SEQUENCE | KRB5_AUTH_CONTEXT_USE_SUBKEY))
1177
0
        goto cleanup;
1178
0
      if (krb_log_exec(krb5glue_auth_con_set_cksumtype, credentials->ctx, context->auth_ctx,
1179
0
                       GSS_CHECKSUM_TYPE))
1180
0
        goto cleanup;
1181
1182
      /* Get a service ticket */
1183
0
      if (krb_log_exec(krb5_sname_to_principal, credentials->ctx, host, sname,
1184
0
                       KRB5_NT_SRV_HST, &in_creds.server))
1185
0
        goto cleanup;
1186
1187
0
      if (krb_log_exec(krb5_cc_get_principal, credentials->ctx, credentials->ccache,
1188
0
                       &in_creds.client))
1189
0
      {
1190
0
        status = SEC_E_WRONG_PRINCIPAL;
1191
0
        goto cleanup;
1192
0
      }
1193
1194
0
      if (krb_log_exec(krb5_get_credentials, credentials->ctx,
1195
0
                       context->u2u ? KRB5_GC_USER_USER : 0, credentials->ccache, &in_creds,
1196
0
                       &creds))
1197
0
      {
1198
0
        status = SEC_E_NO_CREDENTIALS;
1199
0
        goto cleanup;
1200
0
      }
1201
1202
      /* Write the checksum (delegation not implemented) */
1203
0
      cksum.data = cksum_contents;
1204
0
      cksum.length = sizeof(cksum_contents);
1205
0
      winpr_Data_Write_UINT32(cksum_contents, 16);
1206
0
      winpr_Data_Write_UINT32((cksum_contents + 20), context->flags);
1207
1208
0
      if (bindings_buffer)
1209
0
      {
1210
0
        SEC_CHANNEL_BINDINGS* bindings = bindings_buffer->pvBuffer;
1211
1212
        /* Sanity checks */
1213
0
        if (bindings_buffer->cbBuffer < sizeof(SEC_CHANNEL_BINDINGS) ||
1214
0
            (bindings->cbInitiatorLength + bindings->dwInitiatorOffset) >
1215
0
                bindings_buffer->cbBuffer ||
1216
0
            (bindings->cbAcceptorLength + bindings->dwAcceptorOffset) >
1217
0
                bindings_buffer->cbBuffer ||
1218
0
            (bindings->cbApplicationDataLength + bindings->dwApplicationDataOffset) >
1219
0
                bindings_buffer->cbBuffer)
1220
0
        {
1221
0
          status = SEC_E_BAD_BINDINGS;
1222
0
          goto cleanup;
1223
0
        }
1224
1225
0
        md5 = winpr_Digest_New();
1226
0
        if (!md5)
1227
0
          goto cleanup;
1228
1229
0
        if (!winpr_Digest_Init(md5, WINPR_MD_MD5))
1230
0
          goto cleanup;
1231
1232
0
        if (!kerberos_hash_channel_bindings(md5, bindings))
1233
0
          goto cleanup;
1234
1235
0
        if (!winpr_Digest_Final(md5, (BYTE*)cksum_contents + 4, 16))
1236
0
          goto cleanup;
1237
0
      }
1238
1239
      /* Make the AP_REQ message */
1240
0
      if (krb_log_exec(krb5_mk_req_extended, credentials->ctx, &context->auth_ctx, ap_flags,
1241
0
                       &cksum, creds, &output_token))
1242
0
        goto cleanup;
1243
1244
0
      if (!sspi_gss_wrap_token(output_buffer,
1245
0
                               context->u2u ? &kerberos_u2u_OID : &kerberos_OID,
1246
0
                               TOK_ID_AP_REQ, &output_token))
1247
0
        goto cleanup;
1248
1249
0
      if (context->flags & SSPI_GSS_C_SEQUENCE_FLAG)
1250
0
      {
1251
0
        if (krb_log_exec(krb5_auth_con_getlocalseqnumber, credentials->ctx,
1252
0
                         context->auth_ctx, (INT32*)&context->local_seq))
1253
0
          goto cleanup;
1254
0
        context->remote_seq ^= context->local_seq;
1255
0
      }
1256
1257
0
      if (krb_log_exec(krb5glue_update_keyset, credentials->ctx, context->auth_ctx, FALSE,
1258
0
                       &context->keyset))
1259
0
        goto cleanup;
1260
1261
0
      context->state = KERBEROS_STATE_AP_REP;
1262
1263
0
      if (context->flags & SSPI_GSS_C_MUTUAL_FLAG)
1264
0
        status = SEC_I_CONTINUE_NEEDED;
1265
0
      else
1266
0
        status = SEC_E_OK;
1267
0
      break;
1268
1269
0
    case KERBEROS_STATE_AP_REP:
1270
1271
0
      if (tok_id == TOK_ID_AP_REP)
1272
0
      {
1273
0
        if (krb_log_exec(krb5_rd_rep, credentials->ctx, context->auth_ctx, &input_token,
1274
0
                         &reply))
1275
0
          goto cleanup;
1276
0
        krb5_free_ap_rep_enc_part(credentials->ctx, reply);
1277
0
      }
1278
0
      else if (tok_id == TOK_ID_ERROR)
1279
0
      {
1280
0
        krb5glue_log_error(credentials->ctx, &input_token, TAG);
1281
0
        goto cleanup;
1282
0
      }
1283
0
      else
1284
0
        goto bad_token;
1285
1286
0
      if (context->flags & SSPI_GSS_C_SEQUENCE_FLAG)
1287
0
      {
1288
0
        if (krb_log_exec(krb5_auth_con_getremoteseqnumber, credentials->ctx,
1289
0
                         context->auth_ctx, (INT32*)&context->remote_seq))
1290
0
          goto cleanup;
1291
0
      }
1292
1293
0
      if (krb_log_exec(krb5glue_update_keyset, credentials->ctx, context->auth_ctx, FALSE,
1294
0
                       &context->keyset))
1295
0
        goto cleanup;
1296
1297
0
      context->state = KERBEROS_STATE_FINAL;
1298
1299
0
      if (output_buffer)
1300
0
        output_buffer->cbBuffer = 0;
1301
0
      status = SEC_E_OK;
1302
0
      break;
1303
1304
0
    case KERBEROS_STATE_FINAL:
1305
0
    default:
1306
0
      WLog_ERR(TAG, "Kerberos in invalid state!");
1307
0
      goto cleanup;
1308
0
  }
1309
1310
0
cleanup:
1311
0
{
1312
  /* second_ticket is not allocated */
1313
0
  krb5_data edata = WINPR_C_ARRAY_INIT;
1314
0
  in_creds.second_ticket = edata;
1315
0
  krb5_free_cred_contents(credentials->ctx, &in_creds);
1316
0
}
1317
1318
0
  krb5_free_creds(credentials->ctx, creds);
1319
0
  if (output_token.data)
1320
0
    krb5glue_free_data_contents(credentials->ctx, &output_token);
1321
1322
0
  winpr_Digest_Free(md5);
1323
1324
0
  free(target);
1325
1326
0
  if (isNewContext)
1327
0
  {
1328
0
    switch (status)
1329
0
    {
1330
0
      case SEC_E_OK:
1331
0
      case SEC_I_CONTINUE_NEEDED:
1332
0
        sspi_SecureHandleSetLowerPointer(phNewContext, context);
1333
0
        sspi_SecureHandleSetUpperPointer(phNewContext, KERBEROS_SSP_NAME);
1334
0
        break;
1335
0
      default:
1336
0
        kerberos_ContextFree(context, TRUE);
1337
0
        sspi_SecureHandleInvalidate(phNewContext);
1338
0
        break;
1339
0
    }
1340
0
  }
1341
1342
0
  return status;
1343
1344
0
bad_token:
1345
0
  status = SEC_E_INVALID_TOKEN;
1346
0
  goto cleanup;
1347
#else
1348
  return SEC_E_UNSUPPORTED_FUNCTION;
1349
#endif /* WITH_KRB5 */
1350
0
}
1351
1352
static SECURITY_STATUS SEC_ENTRY kerberos_InitializeSecurityContextW(
1353
    PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
1354
    ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
1355
    PCtxtHandle phNewContext, PSecBufferDesc pOutput, ULONG* pfContextAttr, PTimeStamp ptsExpiry)
1356
0
{
1357
0
  SECURITY_STATUS status = 0;
1358
0
  char* target_name = nullptr;
1359
1360
0
  if (pszTargetName)
1361
0
  {
1362
0
    target_name = ConvertWCharToUtf8Alloc(pszTargetName, nullptr);
1363
0
    if (!target_name)
1364
0
      return SEC_E_INSUFFICIENT_MEMORY;
1365
0
  }
1366
1367
0
  status = kerberos_InitializeSecurityContextA(phCredential, phContext, target_name, fContextReq,
1368
0
                                               Reserved1, TargetDataRep, pInput, Reserved2,
1369
0
                                               phNewContext, pOutput, pfContextAttr, ptsExpiry);
1370
1371
0
  if (target_name)
1372
0
    free(target_name);
1373
1374
0
  return status;
1375
0
}
1376
1377
#ifdef WITH_KRB5
1378
static BOOL retrieveTgtForPrincipal(KRB_CREDENTIALS* credentials, krb5_principal principal,
1379
                                    krb5_creds* creds)
1380
0
{
1381
0
  BOOL ret = FALSE;
1382
0
  krb5_kt_cursor cur = WINPR_C_ARRAY_INIT;
1383
0
  krb5_keytab_entry entry = WINPR_C_ARRAY_INIT;
1384
0
  if (krb_log_exec(krb5_kt_start_seq_get, credentials->ctx, credentials->keytab, &cur))
1385
0
    goto cleanup;
1386
1387
0
  do
1388
0
  {
1389
0
    krb5_error_code rv =
1390
0
        krb_log_exec(krb5_kt_next_entry, credentials->ctx, credentials->keytab, &entry, &cur);
1391
0
    if (rv == KRB5_KT_END)
1392
0
      break;
1393
0
    if (rv != 0)
1394
0
      goto cleanup;
1395
1396
0
    if (krb5_principal_compare(credentials->ctx, principal, entry.principal))
1397
0
      break;
1398
0
    rv = krb_log_exec(krb5glue_free_keytab_entry_contents, credentials->ctx, &entry);
1399
0
    memset(&entry, 0, sizeof(entry));
1400
0
    if (rv)
1401
0
      goto cleanup;
1402
0
  } while (1);
1403
1404
0
  if (krb_log_exec(krb5_kt_end_seq_get, credentials->ctx, credentials->keytab, &cur))
1405
0
    goto cleanup;
1406
1407
0
  if (!entry.principal)
1408
0
    goto cleanup;
1409
1410
  /* Get the TGT */
1411
0
  if (krb_log_exec(krb5_get_init_creds_keytab, credentials->ctx, creds, entry.principal,
1412
0
                   credentials->keytab, 0, nullptr, nullptr))
1413
0
    goto cleanup;
1414
1415
0
  ret = TRUE;
1416
1417
0
cleanup:
1418
0
  return ret;
1419
0
}
1420
1421
static BOOL retrieveSomeTgt(KRB_CREDENTIALS* credentials, const char* target, krb5_creds* creds)
1422
0
{
1423
0
  BOOL ret = TRUE;
1424
0
  krb5_principal target_princ = WINPR_C_ARRAY_INIT;
1425
0
  char* default_realm = nullptr;
1426
1427
0
  krb5_error_code rv =
1428
0
      krb_log_exec(krb5_parse_name_flags, credentials->ctx, target, 0, &target_princ);
1429
0
  if (rv)
1430
0
    return FALSE;
1431
1432
#if defined(WITH_KRB5_HEIMDAL)
1433
  if (!target_princ->realm)
1434
  {
1435
    rv = krb_log_exec(krb5_get_default_realm, credentials->ctx, &default_realm);
1436
    if (rv)
1437
      goto out;
1438
1439
    target_princ->realm = default_realm;
1440
  }
1441
#else
1442
0
  if (!target_princ->realm.length)
1443
0
  {
1444
0
    rv = krb_log_exec(krb5_get_default_realm, credentials->ctx, &default_realm);
1445
0
    if (rv)
1446
0
      goto out;
1447
1448
0
    target_princ->realm.data = default_realm;
1449
0
    target_princ->realm.length = (unsigned int)strlen(default_realm);
1450
0
  }
1451
0
#endif
1452
1453
  /*
1454
   * First try with the account service. We were requested with something like
1455
   * TERMSRV/<host>@<realm>, let's see if we have that in our keytab and if we're able
1456
   * to retrieve a TGT with that entry
1457
   *
1458
   */
1459
0
  if (retrieveTgtForPrincipal(credentials, target_princ, creds))
1460
0
    goto out;
1461
1462
0
  ret = FALSE;
1463
1464
0
#if defined(WITH_KRB5_MIT)
1465
  /*
1466
   * if it's not working let's try with <host>$@<REALM> (note the dollar)
1467
   */
1468
0
  {
1469
0
    char hostDollar[300] = WINPR_C_ARRAY_INIT;
1470
0
    if (target_princ->length < 2)
1471
0
      goto out;
1472
1473
0
    (void)snprintf(hostDollar, sizeof(hostDollar) - 1, "%s$@%s", target_princ->data[1].data,
1474
0
                   target_princ->realm.data);
1475
0
    krb5_free_principal(credentials->ctx, target_princ);
1476
1477
0
    rv = krb_log_exec(krb5_parse_name_flags, credentials->ctx, hostDollar, 0, &target_princ);
1478
0
    if (rv)
1479
0
      return FALSE;
1480
0
  }
1481
0
  ret = retrieveTgtForPrincipal(credentials, target_princ, creds);
1482
0
#endif
1483
1484
0
out:
1485
0
  if (default_realm)
1486
0
    krb5_free_default_realm(credentials->ctx, default_realm);
1487
1488
0
  krb5_free_principal(credentials->ctx, target_princ);
1489
0
  return ret;
1490
0
}
1491
#endif
1492
1493
static SECURITY_STATUS SEC_ENTRY kerberos_AcceptSecurityContext(
1494
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED PCtxtHandle phContext,
1495
    WINPR_ATTR_UNUSED PSecBufferDesc pInput, WINPR_ATTR_UNUSED ULONG fContextReq,
1496
    WINPR_ATTR_UNUSED ULONG TargetDataRep, WINPR_ATTR_UNUSED PCtxtHandle phNewContext,
1497
    WINPR_ATTR_UNUSED PSecBufferDesc pOutput, WINPR_ATTR_UNUSED ULONG* pfContextAttr,
1498
    WINPR_ATTR_UNUSED PTimeStamp ptsExpity)
1499
0
{
1500
0
#ifdef WITH_KRB5
1501
0
  BOOL isNewContext = FALSE;
1502
0
  PSecBuffer input_buffer = nullptr;
1503
0
  PSecBuffer output_buffer = nullptr;
1504
0
  WinPrAsn1_OID oid = WINPR_C_ARRAY_INIT;
1505
0
  uint16_t tok_id = 0;
1506
0
  krb5_data input_token = WINPR_C_ARRAY_INIT;
1507
0
  krb5_data output_token = WINPR_C_ARRAY_INIT;
1508
0
  SECURITY_STATUS status = SEC_E_INTERNAL_ERROR;
1509
0
  krb5_flags ap_flags = 0;
1510
0
  krb5glue_authenticator authenticator = nullptr;
1511
0
  char* target = nullptr;
1512
0
  krb5_keytab_entry entry = WINPR_C_ARRAY_INIT;
1513
0
  krb5_creds creds = WINPR_C_ARRAY_INIT;
1514
1515
  /* behave like windows SSPIs that don't want empty context */
1516
0
  if (phContext && !phContext->dwLower && !phContext->dwUpper)
1517
0
    return SEC_E_INVALID_HANDLE;
1518
1519
0
  KRB_CONTEXT* context = sspi_SecureHandleGetLowerPointer(phContext);
1520
0
  KRB_CREDENTIALS* credentials = sspi_SecureHandleGetLowerPointer(phCredential);
1521
1522
0
  if (pInput)
1523
0
    input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
1524
0
  if (pOutput)
1525
0
    output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
1526
1527
0
  if (!input_buffer)
1528
0
    return SEC_E_INVALID_TOKEN;
1529
1530
0
  if (!sspi_gss_unwrap_token(input_buffer, &oid, &tok_id, &input_token))
1531
0
    return SEC_E_INVALID_TOKEN;
1532
1533
0
  if (!context)
1534
0
  {
1535
0
    isNewContext = TRUE;
1536
0
    context = kerberos_ContextNew(credentials);
1537
0
    context->acceptor = TRUE;
1538
1539
0
    if (sspi_gss_oid_compare(&oid, &kerberos_u2u_OID))
1540
0
    {
1541
0
      context->u2u = TRUE;
1542
0
      context->state = KERBEROS_STATE_TGT_REQ;
1543
0
    }
1544
0
    else if (sspi_gss_oid_compare(&oid, &kerberos_OID))
1545
0
      context->state = KERBEROS_STATE_AP_REQ;
1546
0
    else
1547
0
      goto bad_token;
1548
0
  }
1549
0
  else
1550
0
  {
1551
0
    if ((context->u2u && !sspi_gss_oid_compare(&oid, &kerberos_u2u_OID)) ||
1552
0
        (!context->u2u && !sspi_gss_oid_compare(&oid, &kerberos_OID)))
1553
0
      goto bad_token;
1554
0
  }
1555
1556
0
  if (context->state == KERBEROS_STATE_TGT_REQ && tok_id == TOK_ID_TGT_REQ)
1557
0
  {
1558
0
    if (!kerberos_rd_tgt_token(&input_token, &target, nullptr))
1559
0
      goto bad_token;
1560
1561
0
    if (!retrieveSomeTgt(credentials, target, &creds))
1562
0
      goto cleanup;
1563
1564
0
    if (!kerberos_mk_tgt_token(output_buffer, KRB_TGT_REP, nullptr, nullptr, &creds.ticket))
1565
0
      goto cleanup;
1566
1567
0
    if (krb_log_exec(krb5_auth_con_init, credentials->ctx, &context->auth_ctx))
1568
0
      goto cleanup;
1569
1570
0
    if (krb_log_exec(krb5glue_auth_con_setuseruserkey, credentials->ctx, context->auth_ctx,
1571
0
                     &krb5glue_creds_getkey(creds)))
1572
0
      goto cleanup;
1573
1574
0
    context->state = KERBEROS_STATE_AP_REQ;
1575
0
  }
1576
0
  else if (context->state == KERBEROS_STATE_AP_REQ && tok_id == TOK_ID_AP_REQ)
1577
0
  {
1578
0
    if (krb_log_exec(krb5_rd_req, credentials->ctx, &context->auth_ctx, &input_token, nullptr,
1579
0
                     credentials->keytab, &ap_flags, nullptr))
1580
0
      goto cleanup;
1581
1582
0
    if (krb_log_exec(krb5_auth_con_setflags, credentials->ctx, context->auth_ctx,
1583
0
                     KRB5_AUTH_CONTEXT_DO_SEQUENCE | KRB5_AUTH_CONTEXT_USE_SUBKEY))
1584
0
      goto cleanup;
1585
1586
    /* Retrieve and validate the checksum */
1587
0
    if (krb_log_exec(krb5_auth_con_getauthenticator, credentials->ctx, context->auth_ctx,
1588
0
                     &authenticator))
1589
0
      goto cleanup;
1590
0
    if (!krb5glue_authenticator_validate_chksum(authenticator, GSS_CHECKSUM_TYPE,
1591
0
                                                &context->flags))
1592
0
      goto bad_token;
1593
1594
0
    if ((ap_flags & AP_OPTS_MUTUAL_REQUIRED) && (context->flags & SSPI_GSS_C_MUTUAL_FLAG))
1595
0
    {
1596
0
      if (!output_buffer)
1597
0
        goto bad_token;
1598
0
      if (krb_log_exec(krb5_mk_rep, credentials->ctx, context->auth_ctx, &output_token))
1599
0
        goto cleanup;
1600
0
      if (!sspi_gss_wrap_token(output_buffer,
1601
0
                               context->u2u ? &kerberos_u2u_OID : &kerberos_OID,
1602
0
                               TOK_ID_AP_REP, &output_token))
1603
0
        goto cleanup;
1604
0
    }
1605
0
    else
1606
0
    {
1607
0
      if (output_buffer)
1608
0
        output_buffer->cbBuffer = 0;
1609
0
    }
1610
1611
0
    *pfContextAttr = (context->flags & 0x1F);
1612
0
    if (context->flags & SSPI_GSS_C_INTEG_FLAG)
1613
0
      *pfContextAttr |= ASC_RET_INTEGRITY;
1614
1615
0
    if (context->flags & SSPI_GSS_C_SEQUENCE_FLAG)
1616
0
    {
1617
0
      if (krb_log_exec(krb5_auth_con_getlocalseqnumber, credentials->ctx, context->auth_ctx,
1618
0
                       (INT32*)&context->local_seq))
1619
0
        goto cleanup;
1620
0
      if (krb_log_exec(krb5_auth_con_getremoteseqnumber, credentials->ctx, context->auth_ctx,
1621
0
                       (INT32*)&context->remote_seq))
1622
0
        goto cleanup;
1623
0
    }
1624
1625
0
    if (krb_log_exec(krb5glue_update_keyset, credentials->ctx, context->auth_ctx, TRUE,
1626
0
                     &context->keyset))
1627
0
      goto cleanup;
1628
1629
0
    context->state = KERBEROS_STATE_FINAL;
1630
0
  }
1631
0
  else
1632
0
    goto bad_token;
1633
1634
  /* On first call allocate new context */
1635
0
  if (context->state == KERBEROS_STATE_FINAL)
1636
0
    status = SEC_E_OK;
1637
0
  else
1638
0
    status = SEC_I_CONTINUE_NEEDED;
1639
1640
0
cleanup:
1641
0
  free(target);
1642
0
  if (output_token.data)
1643
0
    krb5glue_free_data_contents(credentials->ctx, &output_token);
1644
0
  if (entry.principal)
1645
0
    krb5glue_free_keytab_entry_contents(credentials->ctx, &entry);
1646
1647
0
  if (isNewContext)
1648
0
  {
1649
0
    switch (status)
1650
0
    {
1651
0
      case SEC_E_OK:
1652
0
      case SEC_I_CONTINUE_NEEDED:
1653
0
        sspi_SecureHandleSetLowerPointer(phNewContext, context);
1654
0
        sspi_SecureHandleSetUpperPointer(phNewContext, KERBEROS_SSP_NAME);
1655
0
        break;
1656
0
      default:
1657
0
        kerberos_ContextFree(context, TRUE);
1658
0
        sspi_SecureHandleInvalidate(phNewContext);
1659
0
        break;
1660
0
    }
1661
0
  }
1662
1663
0
  return status;
1664
1665
0
bad_token:
1666
0
  status = SEC_E_INVALID_TOKEN;
1667
0
  goto cleanup;
1668
#else
1669
  return SEC_E_UNSUPPORTED_FUNCTION;
1670
#endif /* WITH_KRB5 */
1671
0
}
1672
1673
#ifdef WITH_KRB5
1674
static KRB_CONTEXT* get_context(PCtxtHandle phContext)
1675
0
{
1676
0
  if (!phContext)
1677
0
    return nullptr;
1678
1679
0
  TCHAR* name = sspi_SecureHandleGetUpperPointer(phContext);
1680
0
  if (!name)
1681
0
    return nullptr;
1682
1683
0
  if (_tcsncmp(KERBEROS_SSP_NAME, name, ARRAYSIZE(KERBEROS_SSP_NAME)) != 0)
1684
0
    return nullptr;
1685
0
  return sspi_SecureHandleGetLowerPointer(phContext);
1686
0
}
1687
1688
static BOOL copy_krb5_data(krb5_data* data, PUCHAR* ptr, ULONG* psize)
1689
0
{
1690
0
  WINPR_ASSERT(data);
1691
0
  WINPR_ASSERT(ptr);
1692
0
  WINPR_ASSERT(psize);
1693
1694
0
  *ptr = (PUCHAR)malloc(data->length);
1695
0
  if (!*ptr)
1696
0
    return FALSE;
1697
1698
0
  *psize = data->length;
1699
0
  memcpy(*ptr, data->data, data->length);
1700
0
  return TRUE;
1701
0
}
1702
#endif
1703
1704
static SECURITY_STATUS
1705
    SEC_ENTRY kerberos_DeleteSecurityContext(WINPR_ATTR_UNUSED PCtxtHandle phContext)
1706
0
{
1707
0
#ifdef WITH_KRB5
1708
0
  KRB_CONTEXT* context = get_context(phContext);
1709
0
  sspi_SecureHandleInvalidate(phContext);
1710
0
  if (!context)
1711
0
    return SEC_E_INVALID_HANDLE;
1712
1713
0
  kerberos_ContextFree(context, TRUE);
1714
1715
0
  return SEC_E_OK;
1716
#else
1717
  return SEC_E_UNSUPPORTED_FUNCTION;
1718
#endif
1719
0
}
1720
1721
#ifdef WITH_KRB5
1722
1723
static SECURITY_STATUS krb5_error_to_SECURITY_STATUS(krb5_error_code code)
1724
0
{
1725
0
  switch (code)
1726
0
  {
1727
0
    case 0:
1728
0
      return SEC_E_OK;
1729
0
    default:
1730
0
      return SEC_E_INTERNAL_ERROR;
1731
0
  }
1732
0
}
1733
1734
static SECURITY_STATUS kerberos_ATTR_SIZES(KRB_CONTEXT* context, KRB_CREDENTIALS* credentials,
1735
                                           SecPkgContext_Sizes* ContextSizes)
1736
0
{
1737
0
  UINT header = 0;
1738
0
  UINT pad = 0;
1739
0
  UINT trailer = 0;
1740
0
  krb5glue_key key = nullptr;
1741
1742
0
  WINPR_ASSERT(context);
1743
0
  WINPR_ASSERT(context->auth_ctx);
1744
1745
  /* The MaxTokenSize by default is 12,000 bytes. This has been the default value
1746
   * since Windows 2000 SP2 and still remains in Windows 7 and Windows 2008 R2.
1747
   *  For Windows Server 2012, the default value of the MaxTokenSize registry
1748
   *  entry is 48,000 bytes.*/
1749
0
  ContextSizes->cbMaxToken = KERBEROS_SecPkgInfoA.cbMaxToken;
1750
0
  ContextSizes->cbMaxSignature = 0;
1751
0
  ContextSizes->cbBlockSize = 1;
1752
0
  ContextSizes->cbSecurityTrailer = 0;
1753
1754
0
  key = get_key(&context->keyset);
1755
1756
0
  if (context->flags & SSPI_GSS_C_CONF_FLAG)
1757
0
  {
1758
0
    krb5_error_code rv = krb_log_exec(krb5glue_crypto_length, credentials->ctx, key,
1759
0
                                      KRB5_CRYPTO_TYPE_HEADER, &header);
1760
0
    if (rv)
1761
0
      return krb5_error_to_SECURITY_STATUS(rv);
1762
1763
0
    rv = krb_log_exec(krb5glue_crypto_length, credentials->ctx, key, KRB5_CRYPTO_TYPE_PADDING,
1764
0
                      &pad);
1765
0
    if (rv)
1766
0
      return krb5_error_to_SECURITY_STATUS(rv);
1767
1768
0
    rv = krb_log_exec(krb5glue_crypto_length, credentials->ctx, key, KRB5_CRYPTO_TYPE_TRAILER,
1769
0
                      &trailer);
1770
0
    if (rv)
1771
0
      return krb5_error_to_SECURITY_STATUS(rv);
1772
1773
    /* GSS header (= 16 bytes) + encrypted header = 32 bytes */
1774
0
    ContextSizes->cbSecurityTrailer = header + pad + trailer + 32;
1775
0
  }
1776
1777
0
  if (context->flags & SSPI_GSS_C_INTEG_FLAG)
1778
0
  {
1779
0
    krb5_error_code rv = krb_log_exec(krb5glue_crypto_length, credentials->ctx, key,
1780
0
                                      KRB5_CRYPTO_TYPE_CHECKSUM, &ContextSizes->cbMaxSignature);
1781
0
    if (rv)
1782
0
      return krb5_error_to_SECURITY_STATUS(rv);
1783
1784
0
    ContextSizes->cbMaxSignature += 16;
1785
0
  }
1786
1787
0
  return SEC_E_OK;
1788
0
}
1789
1790
static SECURITY_STATUS kerberos_ATTR_AUTH_IDENTITY(KRB_CONTEXT* context,
1791
                                                   KRB_CREDENTIALS* credentials,
1792
                                                   SecPkgContext_AuthIdentity* AuthIdentity)
1793
0
{
1794
0
  const SecPkgContext_AuthIdentity empty = WINPR_C_ARRAY_INIT;
1795
1796
0
  WINPR_ASSERT(context);
1797
0
  WINPR_ASSERT(context->auth_ctx);
1798
0
  WINPR_ASSERT(credentials);
1799
1800
0
  WINPR_ASSERT(AuthIdentity);
1801
0
  *AuthIdentity = empty;
1802
1803
0
  krb5glue_authenticator authenticator = nullptr;
1804
0
  krb5_error_code rv = krb_log_exec(krb5_auth_con_getauthenticator, credentials->ctx,
1805
0
                                    context->auth_ctx, &authenticator);
1806
0
  if (rv)
1807
0
    goto fail;
1808
1809
0
  {
1810
0
    rv = -1;
1811
1812
#if defined(WITH_KRB5_HEIMDAL)
1813
    const Realm data = authenticator->crealm;
1814
    if (!data)
1815
      goto fail;
1816
    const size_t data_len = length_Realm(&data);
1817
#else
1818
0
    krb5_data* realm_data = krb5_princ_realm(credentials->ctx, authenticator->client);
1819
0
    if (!realm_data)
1820
0
      goto fail;
1821
0
    const char* data = realm_data->data;
1822
0
    if (!data)
1823
0
      goto fail;
1824
0
    const size_t data_len = realm_data->length;
1825
0
#endif
1826
1827
0
    if (data_len > (sizeof(AuthIdentity->Domain) - 1))
1828
0
      goto fail;
1829
0
    strncpy(AuthIdentity->Domain, data, data_len);
1830
0
  }
1831
1832
0
  {
1833
#if defined(WITH_KRB5_HEIMDAL)
1834
    const PrincipalName* principal = &authenticator->cname;
1835
    const size_t name_length = length_PrincipalName(principal);
1836
    if (!principal->name_string.val)
1837
      goto fail;
1838
    const char* name = *principal->name_string.val;
1839
#else
1840
0
    char* name = nullptr;
1841
0
    rv = krb_log_exec(krb5_unparse_name_flags, credentials->ctx, authenticator->client,
1842
0
                      KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
1843
0
    if (rv)
1844
0
      goto fail;
1845
1846
0
    const size_t name_length = strlen(name);
1847
0
#endif
1848
1849
0
    const bool ok = (name_length <= (sizeof(AuthIdentity->User) - 1));
1850
0
    if (ok)
1851
0
      strncpy(AuthIdentity->User, name, name_length);
1852
1853
0
    rv = ok ? 0 : -1;
1854
1855
0
#if !defined(WITH_KRB5_HEIMDAL)
1856
0
    krb5_free_unparsed_name(credentials->ctx, name);
1857
0
#endif
1858
0
  }
1859
1860
0
fail:
1861
0
  krb5glue_free_authenticator(credentials->ctx, authenticator);
1862
0
  return krb5_error_to_SECURITY_STATUS(rv);
1863
0
}
1864
1865
static SECURITY_STATUS kerberos_ATTR_PACKAGE_INFO_A(WINPR_ATTR_UNUSED KRB_CONTEXT* context,
1866
                                                    WINPR_ATTR_UNUSED KRB_CREDENTIALS* credentials,
1867
                                                    SecPkgContext_PackageInfoA* PackageInfo)
1868
0
{
1869
0
  size_t size = sizeof(SecPkgInfoA);
1870
0
  SecPkgInfoA* pPackageInfo =
1871
0
      (SecPkgInfoA*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1872
1873
0
  if (!pPackageInfo)
1874
0
    return SEC_E_INSUFFICIENT_MEMORY;
1875
1876
0
  pPackageInfo->fCapabilities = KERBEROS_SecPkgInfoA.fCapabilities;
1877
0
  pPackageInfo->wVersion = KERBEROS_SecPkgInfoA.wVersion;
1878
0
  pPackageInfo->wRPCID = KERBEROS_SecPkgInfoA.wRPCID;
1879
0
  pPackageInfo->cbMaxToken = KERBEROS_SecPkgInfoA.cbMaxToken;
1880
0
  pPackageInfo->Name = _strdup(KERBEROS_SecPkgInfoA.Name);
1881
0
  pPackageInfo->Comment = _strdup(KERBEROS_SecPkgInfoA.Comment);
1882
1883
0
  if (!pPackageInfo->Name || !pPackageInfo->Comment)
1884
0
  {
1885
0
    sspi_ContextBufferFree(pPackageInfo);
1886
0
    return SEC_E_INSUFFICIENT_MEMORY;
1887
0
  }
1888
0
  PackageInfo->PackageInfo = pPackageInfo;
1889
0
  return SEC_E_OK;
1890
0
}
1891
1892
static SECURITY_STATUS kerberos_ATTR_PACKAGE_INFO_W(WINPR_ATTR_UNUSED KRB_CONTEXT* context,
1893
                                                    WINPR_ATTR_UNUSED KRB_CREDENTIALS* credentials,
1894
                                                    SecPkgContext_PackageInfoW* PackageInfo)
1895
0
{
1896
0
  size_t size = sizeof(SecPkgInfoW);
1897
0
  SecPkgInfoW* pPackageInfo =
1898
0
      (SecPkgInfoW*)sspi_ContextBufferAlloc(QuerySecurityPackageInfoIndex, size);
1899
1900
0
  if (!pPackageInfo)
1901
0
    return SEC_E_INSUFFICIENT_MEMORY;
1902
1903
0
  pPackageInfo->fCapabilities = KERBEROS_SecPkgInfoW.fCapabilities;
1904
0
  pPackageInfo->wVersion = KERBEROS_SecPkgInfoW.wVersion;
1905
0
  pPackageInfo->wRPCID = KERBEROS_SecPkgInfoW.wRPCID;
1906
0
  pPackageInfo->cbMaxToken = KERBEROS_SecPkgInfoW.cbMaxToken;
1907
0
  pPackageInfo->Name = _wcsdup(KERBEROS_SecPkgInfoW.Name);
1908
0
  pPackageInfo->Comment = _wcsdup(KERBEROS_SecPkgInfoW.Comment);
1909
1910
0
  if (!pPackageInfo->Name || !pPackageInfo->Comment)
1911
0
  {
1912
0
    sspi_ContextBufferFree(pPackageInfo);
1913
0
    return SEC_E_INSUFFICIENT_MEMORY;
1914
0
  }
1915
0
  PackageInfo->PackageInfo = pPackageInfo;
1916
0
  return SEC_E_OK;
1917
0
}
1918
1919
static SECURITY_STATUS kerberos_ATTR_TICKET_LOGON(KRB_CONTEXT* context,
1920
                                                  KRB_CREDENTIALS* credentials,
1921
                                                  KERB_TICKET_LOGON* ticketLogon)
1922
0
{
1923
0
  krb5_creds matchCred = WINPR_C_ARRAY_INIT;
1924
0
  krb5_auth_context authContext = nullptr;
1925
0
  krb5_flags getCredsFlags = KRB5_GC_CACHED;
1926
0
  BOOL firstRun = TRUE;
1927
0
  krb5_creds* hostCred = nullptr;
1928
0
  SECURITY_STATUS ret = SEC_E_INSUFFICIENT_MEMORY;
1929
0
  int rv = krb_log_exec(krb5_sname_to_principal, credentials->ctx, context->targetHost, "HOST",
1930
0
                        KRB5_NT_SRV_HST, &matchCred.server);
1931
0
  if (rv)
1932
0
    goto out;
1933
1934
0
  rv = krb_log_exec(krb5_cc_get_principal, credentials->ctx, credentials->ccache,
1935
0
                    &matchCred.client);
1936
0
  if (rv)
1937
0
    goto out;
1938
1939
  /* try from the cache first, and then do a new request */
1940
0
again:
1941
0
  rv = krb_log_exec(krb5_get_credentials, credentials->ctx, getCredsFlags, credentials->ccache,
1942
0
                    &matchCred, &hostCred);
1943
0
  switch (rv)
1944
0
  {
1945
0
    case 0:
1946
0
      break;
1947
0
    case KRB5_CC_NOTFOUND:
1948
0
      getCredsFlags = 0;
1949
0
      if (firstRun)
1950
0
      {
1951
0
        firstRun = FALSE;
1952
0
        goto again;
1953
0
      }
1954
0
      WINPR_FALLTHROUGH
1955
0
    default:
1956
0
      WLog_ERR(TAG, "krb5_get_credentials(hostCreds), rv=%d", rv);
1957
0
      goto out;
1958
0
  }
1959
1960
0
  if (krb_log_exec(krb5_auth_con_init, credentials->ctx, &authContext))
1961
0
    goto out;
1962
1963
0
  {
1964
0
    krb5_data derOut = WINPR_C_ARRAY_INIT;
1965
0
    if (krb_log_exec(krb5_fwd_tgt_creds, credentials->ctx, authContext, context->targetHost,
1966
0
                     matchCred.client, matchCred.server, credentials->ccache, 1, &derOut))
1967
0
    {
1968
0
      ret = SEC_E_LOGON_DENIED;
1969
0
      goto out;
1970
0
    }
1971
1972
0
    ticketLogon->MessageType = KerbTicketLogon;
1973
0
    ticketLogon->Flags = KERB_LOGON_FLAG_REDIRECTED;
1974
1975
0
    if (!copy_krb5_data(&hostCred->ticket, &ticketLogon->ServiceTicket,
1976
0
                        &ticketLogon->ServiceTicketLength))
1977
0
    {
1978
0
      krb5_free_data(credentials->ctx, &derOut);
1979
0
      goto out;
1980
0
    }
1981
1982
0
    ticketLogon->TicketGrantingTicketLength = derOut.length;
1983
0
    ticketLogon->TicketGrantingTicket = (PUCHAR)derOut.data;
1984
0
  }
1985
1986
0
  ret = SEC_E_OK;
1987
0
out:
1988
0
  krb5_auth_con_free(credentials->ctx, authContext);
1989
0
  krb5_free_creds(credentials->ctx, hostCred);
1990
0
  krb5_free_cred_contents(credentials->ctx, &matchCred);
1991
0
  return ret;
1992
0
}
1993
1994
#endif /* WITH_KRB5 */
1995
1996
WINPR_ATTR_NODISCARD
1997
static SECURITY_STATUS SEC_ENTRY kerberos_QueryContextAttributesCommon(
1998
    PCtxtHandle phContext, WINPR_ATTR_UNUSED ULONG ulAttribute, void* pBuffer)
1999
0
{
2000
0
  if (!phContext)
2001
0
    return SEC_E_INVALID_HANDLE;
2002
2003
0
  if (!pBuffer)
2004
0
    return SEC_E_INVALID_PARAMETER;
2005
2006
0
#ifdef WITH_KRB5
2007
0
  KRB_CONTEXT* context = get_context(phContext);
2008
0
  if (!context)
2009
0
    return SEC_E_INVALID_PARAMETER;
2010
2011
0
  KRB_CREDENTIALS* credentials = context->credentials;
2012
2013
0
  switch (ulAttribute)
2014
0
  {
2015
0
    case SECPKG_ATTR_SIZES:
2016
0
      return kerberos_ATTR_SIZES(context, credentials, (SecPkgContext_Sizes*)pBuffer);
2017
2018
0
    case SECPKG_ATTR_AUTH_IDENTITY:
2019
0
      return kerberos_ATTR_AUTH_IDENTITY(context, credentials,
2020
0
                                         (SecPkgContext_AuthIdentity*)pBuffer);
2021
2022
0
    case SECPKG_CRED_ATTR_TICKET_LOGON:
2023
0
      return kerberos_ATTR_TICKET_LOGON(context, credentials, (KERB_TICKET_LOGON*)pBuffer);
2024
2025
0
    default:
2026
0
      WLog_ERR(TAG, "TODO: QueryContextAttributes implement ulAttribute=0x%08" PRIx32,
2027
0
               ulAttribute);
2028
0
      return SEC_E_UNSUPPORTED_FUNCTION;
2029
0
  }
2030
#else
2031
  return SEC_E_UNSUPPORTED_FUNCTION;
2032
#endif
2033
0
}
2034
2035
static SECURITY_STATUS SEC_ENTRY kerberos_QueryContextAttributesA(
2036
    PCtxtHandle phContext, WINPR_ATTR_UNUSED ULONG ulAttribute, void* pBuffer)
2037
0
{
2038
0
  if (!phContext)
2039
0
    return SEC_E_INVALID_HANDLE;
2040
2041
0
  if (!pBuffer)
2042
0
    return SEC_E_INVALID_PARAMETER;
2043
2044
0
#ifdef WITH_KRB5
2045
0
  KRB_CONTEXT* context = get_context(phContext);
2046
0
  if (!context)
2047
0
    return SEC_E_INVALID_PARAMETER;
2048
2049
0
  KRB_CREDENTIALS* credentials = context->credentials;
2050
2051
0
  switch (ulAttribute)
2052
0
  {
2053
0
    case SECPKG_ATTR_PACKAGE_INFO:
2054
0
      return kerberos_ATTR_PACKAGE_INFO_A(context, credentials,
2055
0
                                          (SecPkgContext_PackageInfoA*)pBuffer);
2056
0
    default:
2057
0
      break;
2058
0
  }
2059
0
#endif
2060
0
  return kerberos_QueryContextAttributesCommon(phContext, ulAttribute, pBuffer);
2061
0
}
2062
2063
static SECURITY_STATUS SEC_ENTRY kerberos_QueryContextAttributesW(PCtxtHandle phContext,
2064
                                                                  ULONG ulAttribute, void* pBuffer)
2065
0
{
2066
0
  if (!phContext)
2067
0
    return SEC_E_INVALID_HANDLE;
2068
2069
0
  if (!pBuffer)
2070
0
    return SEC_E_INVALID_PARAMETER;
2071
2072
0
#ifdef WITH_KRB5
2073
0
  KRB_CONTEXT* context = get_context(phContext);
2074
0
  if (!context)
2075
0
    return SEC_E_INVALID_PARAMETER;
2076
2077
0
  KRB_CREDENTIALS* credentials = context->credentials;
2078
2079
0
  switch (ulAttribute)
2080
0
  {
2081
0
    case SECPKG_ATTR_PACKAGE_INFO:
2082
0
      return kerberos_ATTR_PACKAGE_INFO_W(context, credentials,
2083
0
                                          (SecPkgContext_PackageInfoW*)pBuffer);
2084
0
    default:
2085
0
      break;
2086
0
  }
2087
0
#endif
2088
0
  return kerberos_QueryContextAttributesCommon(phContext, ulAttribute, pBuffer);
2089
0
}
2090
2091
static SECURITY_STATUS SEC_ENTRY kerberos_SetContextAttributesW(
2092
    WINPR_ATTR_UNUSED PCtxtHandle phContext, WINPR_ATTR_UNUSED ULONG ulAttribute,
2093
    WINPR_ATTR_UNUSED void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer)
2094
0
{
2095
0
  return SEC_E_UNSUPPORTED_FUNCTION;
2096
0
}
2097
2098
static SECURITY_STATUS SEC_ENTRY kerberos_SetContextAttributesA(
2099
    WINPR_ATTR_UNUSED PCtxtHandle phContext, WINPR_ATTR_UNUSED ULONG ulAttribute,
2100
    WINPR_ATTR_UNUSED void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer)
2101
0
{
2102
0
  return SEC_E_UNSUPPORTED_FUNCTION;
2103
0
}
2104
2105
static SECURITY_STATUS SEC_ENTRY kerberos_SetCredentialsAttributesX(
2106
    WINPR_ATTR_UNUSED PCredHandle phCredential, WINPR_ATTR_UNUSED ULONG ulAttribute,
2107
    WINPR_ATTR_UNUSED void* pBuffer, WINPR_ATTR_UNUSED ULONG cbBuffer,
2108
    WINPR_ATTR_UNUSED BOOL unicode)
2109
0
{
2110
0
#ifdef WITH_KRB5
2111
0
  KRB_CREDENTIALS* credentials = nullptr;
2112
2113
0
  if (!phCredential)
2114
0
    return SEC_E_INVALID_HANDLE;
2115
2116
0
  credentials = sspi_SecureHandleGetLowerPointer(phCredential);
2117
2118
0
  if (!credentials)
2119
0
    return SEC_E_INVALID_HANDLE;
2120
2121
0
  if (!pBuffer)
2122
0
    return SEC_E_INSUFFICIENT_MEMORY;
2123
2124
0
  switch (ulAttribute)
2125
0
  {
2126
0
    case SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS:
2127
0
    {
2128
0
      SecPkgCredentials_KdcProxySettingsW* kdc_settings = pBuffer;
2129
2130
      /* Sanity checks */
2131
0
      if (cbBuffer < sizeof(SecPkgCredentials_KdcProxySettingsW) ||
2132
0
          kdc_settings->Version != KDC_PROXY_SETTINGS_V1 ||
2133
0
          kdc_settings->ProxyServerOffset < sizeof(SecPkgCredentials_KdcProxySettingsW) ||
2134
0
          cbBuffer < sizeof(SecPkgCredentials_KdcProxySettingsW) +
2135
0
                         kdc_settings->ProxyServerOffset + kdc_settings->ProxyServerLength)
2136
0
        return SEC_E_INVALID_TOKEN;
2137
2138
0
      if (credentials->kdc_url)
2139
0
      {
2140
0
        free(credentials->kdc_url);
2141
0
        credentials->kdc_url = nullptr;
2142
0
      }
2143
2144
0
      if (kdc_settings->ProxyServerLength > 0)
2145
0
      {
2146
0
        WCHAR* proxy = (WCHAR*)((BYTE*)pBuffer + kdc_settings->ProxyServerOffset);
2147
2148
0
        credentials->kdc_url = ConvertWCharNToUtf8Alloc(
2149
0
            proxy, kdc_settings->ProxyServerLength / sizeof(WCHAR), nullptr);
2150
0
        if (!credentials->kdc_url)
2151
0
          return SEC_E_INSUFFICIENT_MEMORY;
2152
0
      }
2153
2154
0
      return SEC_E_OK;
2155
0
    }
2156
0
    case SECPKG_CRED_ATTR_NAMES:
2157
0
    case SECPKG_ATTR_SUPPORTED_ALGS:
2158
0
    default:
2159
0
      WLog_ERR(TAG, "TODO: SetCredentialsAttributesX implement ulAttribute=0x%08" PRIx32,
2160
0
               ulAttribute);
2161
0
      return SEC_E_UNSUPPORTED_FUNCTION;
2162
0
  }
2163
2164
#else
2165
  return SEC_E_UNSUPPORTED_FUNCTION;
2166
#endif
2167
0
}
2168
2169
static SECURITY_STATUS SEC_ENTRY kerberos_SetCredentialsAttributesW(PCredHandle phCredential,
2170
                                                                    ULONG ulAttribute,
2171
                                                                    void* pBuffer, ULONG cbBuffer)
2172
0
{
2173
0
  return kerberos_SetCredentialsAttributesX(phCredential, ulAttribute, pBuffer, cbBuffer, TRUE);
2174
0
}
2175
2176
static SECURITY_STATUS SEC_ENTRY kerberos_SetCredentialsAttributesA(PCredHandle phCredential,
2177
                                                                    ULONG ulAttribute,
2178
                                                                    void* pBuffer, ULONG cbBuffer)
2179
0
{
2180
0
  return kerberos_SetCredentialsAttributesX(phCredential, ulAttribute, pBuffer, cbBuffer, FALSE);
2181
0
}
2182
2183
static SECURITY_STATUS SEC_ENTRY kerberos_EncryptMessage(WINPR_ATTR_UNUSED PCtxtHandle phContext,
2184
                                                         WINPR_ATTR_UNUSED ULONG fQOP,
2185
                                                         WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
2186
                                                         WINPR_ATTR_UNUSED ULONG MessageSeqNo)
2187
0
{
2188
0
#ifdef WITH_KRB5
2189
0
  KRB_CONTEXT* context = get_context(phContext);
2190
0
  PSecBuffer sig_buffer = nullptr;
2191
0
  PSecBuffer data_buffer = nullptr;
2192
0
  char* header = nullptr;
2193
0
  BYTE flags = 0;
2194
0
  krb5glue_key key = nullptr;
2195
0
  krb5_keyusage usage = 0;
2196
0
  krb5_crypto_iov encrypt_iov[] = { { KRB5_CRYPTO_TYPE_HEADER, WINPR_C_ARRAY_INIT },
2197
0
                                  { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2198
0
                                  { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2199
0
                                  { KRB5_CRYPTO_TYPE_PADDING, WINPR_C_ARRAY_INIT },
2200
0
                                  { KRB5_CRYPTO_TYPE_TRAILER, WINPR_C_ARRAY_INIT } };
2201
2202
0
  if (!context)
2203
0
    return SEC_E_INVALID_HANDLE;
2204
2205
0
  if (!(context->flags & SSPI_GSS_C_CONF_FLAG))
2206
0
    return SEC_E_UNSUPPORTED_FUNCTION;
2207
2208
0
  KRB_CREDENTIALS* creds = context->credentials;
2209
2210
0
  sig_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_TOKEN);
2211
0
  data_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA);
2212
2213
0
  if (!sig_buffer || !data_buffer)
2214
0
    return SEC_E_INVALID_TOKEN;
2215
2216
0
  if (fQOP)
2217
0
    return SEC_E_QOP_NOT_SUPPORTED;
2218
2219
0
  flags |= context->acceptor ? FLAG_SENDER_IS_ACCEPTOR : 0;
2220
0
  flags |= FLAG_WRAP_CONFIDENTIAL;
2221
2222
0
  key = get_key(&context->keyset);
2223
0
  if (!key)
2224
0
    return SEC_E_INTERNAL_ERROR;
2225
2226
0
  flags |= context->keyset.acceptor_key == key ? FLAG_ACCEPTOR_SUBKEY : 0;
2227
2228
0
  usage = context->acceptor ? KG_USAGE_ACCEPTOR_SEAL : KG_USAGE_INITIATOR_SEAL;
2229
2230
  /* Set the lengths of the data (plaintext + header) */
2231
0
  encrypt_iov[1].data.length = data_buffer->cbBuffer;
2232
0
  encrypt_iov[2].data.length = 16;
2233
2234
  /* Get the lengths of the header, trailer, and padding and ensure sig_buffer is large enough */
2235
0
  if (krb_log_exec(krb5glue_crypto_length_iov, creds->ctx, key, encrypt_iov,
2236
0
                   ARRAYSIZE(encrypt_iov)))
2237
0
    return SEC_E_INTERNAL_ERROR;
2238
0
  if (sig_buffer->cbBuffer <
2239
0
      encrypt_iov[0].data.length + encrypt_iov[3].data.length + encrypt_iov[4].data.length + 32)
2240
0
    return SEC_E_INSUFFICIENT_MEMORY;
2241
2242
  /* Set up the iov array in sig_buffer */
2243
0
  header = sig_buffer->pvBuffer;
2244
0
  encrypt_iov[2].data.data = header + 16;
2245
0
  encrypt_iov[3].data.data = encrypt_iov[2].data.data + encrypt_iov[2].data.length;
2246
0
  encrypt_iov[4].data.data = encrypt_iov[3].data.data + encrypt_iov[3].data.length;
2247
0
  encrypt_iov[0].data.data = encrypt_iov[4].data.data + encrypt_iov[4].data.length;
2248
0
  encrypt_iov[1].data.data = data_buffer->pvBuffer;
2249
2250
  /* Write the GSS header with 0 in RRC */
2251
0
  winpr_Data_Write_UINT16_BE(header, TOK_ID_WRAP);
2252
0
  header[2] = WINPR_ASSERTING_INT_CAST(char, flags);
2253
0
  header[3] = (char)0xFF;
2254
0
  winpr_Data_Write_UINT32(header + 4, 0);
2255
0
  winpr_Data_Write_UINT64_BE(header + 8, (context->local_seq + MessageSeqNo));
2256
2257
  /* Copy header to be encrypted */
2258
0
  CopyMemory(encrypt_iov[2].data.data, header, 16);
2259
2260
  /* Set the correct RRC */
2261
0
  const size_t len = 16 + encrypt_iov[3].data.length + encrypt_iov[4].data.length;
2262
0
  winpr_Data_Write_UINT16_BE(header + 6, WINPR_ASSERTING_INT_CAST(UINT16, len));
2263
2264
0
  if (krb_log_exec(krb5glue_encrypt_iov, creds->ctx, key, usage, encrypt_iov,
2265
0
                   ARRAYSIZE(encrypt_iov)))
2266
0
    return SEC_E_INTERNAL_ERROR;
2267
2268
0
  return SEC_E_OK;
2269
#else
2270
  return SEC_E_UNSUPPORTED_FUNCTION;
2271
#endif
2272
0
}
2273
2274
static SECURITY_STATUS SEC_ENTRY kerberos_DecryptMessage(WINPR_ATTR_UNUSED PCtxtHandle phContext,
2275
                                                         WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
2276
                                                         WINPR_ATTR_UNUSED ULONG MessageSeqNo,
2277
                                                         WINPR_ATTR_UNUSED ULONG* pfQOP)
2278
0
{
2279
0
#ifdef WITH_KRB5
2280
0
  KRB_CONTEXT* context = get_context(phContext);
2281
0
  if (!context)
2282
0
    return SEC_E_INVALID_HANDLE;
2283
2284
0
  if (!(context->flags & SSPI_GSS_C_CONF_FLAG))
2285
0
    return SEC_E_UNSUPPORTED_FUNCTION;
2286
2287
0
  KRB_CREDENTIALS* creds = context->credentials;
2288
2289
0
  const PSecBuffer sig_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_TOKEN);
2290
0
  PSecBuffer data_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA);
2291
2292
0
  if (!sig_buffer || !data_buffer || sig_buffer->cbBuffer < 16)
2293
0
    return SEC_E_INVALID_TOKEN;
2294
2295
  /* Read in header information */
2296
0
  const BYTE* header = sig_buffer->pvBuffer;
2297
0
  const uint16_t tok_id = winpr_Data_Get_UINT16_BE(header);
2298
0
  const BYTE flags = header[2];
2299
0
  const uint16_t ec = winpr_Data_Get_UINT16_BE(&header[4]);
2300
0
  const uint16_t rrc = winpr_Data_Get_UINT16_BE(&header[6]);
2301
0
  const uint64_t seq_no = winpr_Data_Get_UINT64_BE(&header[8]);
2302
2303
  /* Check that the header is valid */
2304
0
  if ((tok_id != TOK_ID_WRAP) || (header[3] != 0xFF))
2305
0
    return SEC_E_INVALID_TOKEN;
2306
2307
0
  if ((flags & FLAG_SENDER_IS_ACCEPTOR) == context->acceptor)
2308
0
    return SEC_E_INVALID_TOKEN;
2309
2310
0
  if ((context->flags & ISC_REQ_SEQUENCE_DETECT) &&
2311
0
      (seq_no != context->remote_seq + MessageSeqNo))
2312
0
    return SEC_E_OUT_OF_SEQUENCE;
2313
2314
0
  if (!(flags & FLAG_WRAP_CONFIDENTIAL))
2315
0
    return SEC_E_INVALID_TOKEN;
2316
2317
  /* We don't expect a trailer buffer; the encrypted header must be rotated */
2318
0
  if (rrc < 16)
2319
0
    return SEC_E_INVALID_TOKEN;
2320
2321
  /* Find the proper key and key usage */
2322
0
  krb5glue_key key = get_key(&context->keyset);
2323
0
  if (!key || ((flags & FLAG_ACCEPTOR_SUBKEY) && (context->keyset.acceptor_key != key)))
2324
0
    return SEC_E_INTERNAL_ERROR;
2325
0
  krb5_keyusage usage = context->acceptor ? KG_USAGE_INITIATOR_SEAL : KG_USAGE_ACCEPTOR_SEAL;
2326
2327
  /* Fill in the lengths of the iov array */
2328
0
  krb5_crypto_iov iov[] = { { KRB5_CRYPTO_TYPE_HEADER, WINPR_C_ARRAY_INIT },
2329
0
                          { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2330
0
                          { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2331
0
                          { KRB5_CRYPTO_TYPE_PADDING, WINPR_C_ARRAY_INIT },
2332
0
                          { KRB5_CRYPTO_TYPE_TRAILER, WINPR_C_ARRAY_INIT } };
2333
0
  iov[1].data.length = data_buffer->cbBuffer;
2334
0
  iov[2].data.length = 16;
2335
0
  if (krb_log_exec(krb5glue_crypto_length_iov, creds->ctx, key, iov, ARRAYSIZE(iov)))
2336
0
    return SEC_E_INTERNAL_ERROR;
2337
2338
  /* We don't expect a trailer buffer; everything must be in sig_buffer */
2339
0
  if (rrc != 16 + iov[3].data.length + iov[4].data.length)
2340
0
    return SEC_E_INVALID_TOKEN;
2341
0
  if (sig_buffer->cbBuffer != 16 + rrc + iov[0].data.length)
2342
0
    return SEC_E_INVALID_TOKEN;
2343
2344
  /* Locate the parts of the message */
2345
0
  const size_t iov0Offset = 16ull + rrc + ec;
2346
0
  if (iov0Offset + iov[0].data.length > sig_buffer->cbBuffer)
2347
0
    return SEC_E_INVALID_TOKEN;
2348
2349
0
  const size_t iov2Offset = 16ull + ec;
2350
0
  if (iov2Offset + iov[2].data.length > sig_buffer->cbBuffer)
2351
0
    return SEC_E_INVALID_TOKEN;
2352
2353
0
  iov[0].data.data = WINPR_CAST_CONST_PTR_AWAY(&header[iov0Offset], char*);
2354
0
  iov[1].data.data = data_buffer->pvBuffer;
2355
0
  iov[2].data.data = WINPR_CAST_CONST_PTR_AWAY(&header[iov2Offset], char*);
2356
0
  char* data2 = iov[2].data.data;
2357
0
  iov[3].data.data = &data2[iov[2].data.length];
2358
2359
0
  char* data3 = iov[3].data.data;
2360
0
  iov[4].data.data = &data3[iov[3].data.length];
2361
2362
0
  if (krb_log_exec(krb5glue_decrypt_iov, creds->ctx, key, usage, iov, ARRAYSIZE(iov)))
2363
0
    return SEC_E_INTERNAL_ERROR;
2364
2365
  /* Validate the encrypted header */
2366
0
  winpr_Data_Write_UINT16_BE(iov[2].data.data + 4, ec);
2367
0
  winpr_Data_Write_UINT16_BE(iov[2].data.data + 6, rrc);
2368
0
  if (memcmp(iov[2].data.data, header, 16) != 0)
2369
0
    return SEC_E_MESSAGE_ALTERED;
2370
2371
0
  *pfQOP = 0;
2372
2373
0
  return SEC_E_OK;
2374
#else
2375
  return SEC_E_UNSUPPORTED_FUNCTION;
2376
#endif
2377
0
}
2378
2379
static SECURITY_STATUS SEC_ENTRY kerberos_MakeSignature(WINPR_ATTR_UNUSED PCtxtHandle phContext,
2380
                                                        WINPR_ATTR_UNUSED ULONG fQOP,
2381
                                                        WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
2382
                                                        WINPR_ATTR_UNUSED ULONG MessageSeqNo)
2383
0
{
2384
0
#ifdef WITH_KRB5
2385
0
  KRB_CONTEXT* context = get_context(phContext);
2386
0
  PSecBuffer sig_buffer = nullptr;
2387
0
  PSecBuffer data_buffer = nullptr;
2388
0
  krb5glue_key key = nullptr;
2389
0
  krb5_keyusage usage = 0;
2390
0
  BYTE flags = 0;
2391
0
  krb5_crypto_iov iov[] = { { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2392
0
                          { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2393
0
                          { KRB5_CRYPTO_TYPE_CHECKSUM, WINPR_C_ARRAY_INIT } };
2394
2395
0
  if (!context)
2396
0
    return SEC_E_INVALID_HANDLE;
2397
2398
0
  if (!(context->flags & SSPI_GSS_C_INTEG_FLAG))
2399
0
    return SEC_E_UNSUPPORTED_FUNCTION;
2400
2401
0
  KRB_CREDENTIALS* creds = context->credentials;
2402
2403
0
  sig_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_TOKEN);
2404
0
  data_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA);
2405
2406
0
  if (!sig_buffer || !data_buffer)
2407
0
    return SEC_E_INVALID_TOKEN;
2408
2409
0
  flags |= context->acceptor ? FLAG_SENDER_IS_ACCEPTOR : 0;
2410
2411
0
  key = get_key(&context->keyset);
2412
0
  if (!key)
2413
0
    return SEC_E_INTERNAL_ERROR;
2414
0
  usage = context->acceptor ? KG_USAGE_ACCEPTOR_SIGN : KG_USAGE_INITIATOR_SIGN;
2415
2416
0
  flags |= context->keyset.acceptor_key == key ? FLAG_ACCEPTOR_SUBKEY : 0;
2417
2418
  /* Fill in the lengths of the iov array */
2419
0
  iov[0].data.length = data_buffer->cbBuffer;
2420
0
  iov[1].data.length = 16;
2421
0
  if (krb_log_exec(krb5glue_crypto_length_iov, creds->ctx, key, iov, ARRAYSIZE(iov)))
2422
0
    return SEC_E_INTERNAL_ERROR;
2423
2424
  /* Ensure the buffer is big enough */
2425
0
  if (sig_buffer->cbBuffer < iov[2].data.length + 16)
2426
0
    return SEC_E_INSUFFICIENT_MEMORY;
2427
2428
  /* Write the header */
2429
0
  char* header = sig_buffer->pvBuffer;
2430
0
  winpr_Data_Write_UINT16_BE(header, TOK_ID_MIC);
2431
0
  header[2] = WINPR_ASSERTING_INT_CAST(char, flags);
2432
0
  memset(header + 3, 0xFF, 5);
2433
0
  winpr_Data_Write_UINT64_BE(header + 8, (context->local_seq + MessageSeqNo));
2434
2435
  /* Set up the iov array */
2436
0
  iov[0].data.data = data_buffer->pvBuffer;
2437
0
  iov[1].data.data = header;
2438
0
  iov[2].data.data = header + 16;
2439
2440
0
  if (krb_log_exec(krb5glue_make_checksum_iov, creds->ctx, key, usage, iov, ARRAYSIZE(iov)))
2441
0
    return SEC_E_INTERNAL_ERROR;
2442
2443
0
  sig_buffer->cbBuffer = iov[2].data.length + 16;
2444
2445
0
  return SEC_E_OK;
2446
#else
2447
  return SEC_E_UNSUPPORTED_FUNCTION;
2448
#endif
2449
0
}
2450
2451
static SECURITY_STATUS SEC_ENTRY kerberos_VerifySignature(WINPR_ATTR_UNUSED PCtxtHandle phContext,
2452
                                                          WINPR_ATTR_UNUSED PSecBufferDesc pMessage,
2453
                                                          WINPR_ATTR_UNUSED ULONG MessageSeqNo,
2454
                                                          WINPR_ATTR_UNUSED ULONG* pfQOP)
2455
0
{
2456
0
#ifdef WITH_KRB5
2457
0
  PSecBuffer sig_buffer = nullptr;
2458
0
  PSecBuffer data_buffer = nullptr;
2459
0
  krb5glue_key key = nullptr;
2460
0
  krb5_keyusage usage = 0;
2461
0
  BYTE flags = 0;
2462
0
  uint16_t tok_id = 0;
2463
0
  uint64_t seq_no = 0;
2464
0
  krb5_boolean is_valid = 0;
2465
0
  krb5_crypto_iov iov[] = { { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2466
0
                          { KRB5_CRYPTO_TYPE_DATA, WINPR_C_ARRAY_INIT },
2467
0
                          { KRB5_CRYPTO_TYPE_CHECKSUM, WINPR_C_ARRAY_INIT } };
2468
0
  BYTE cmp_filler[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2469
2470
0
  KRB_CONTEXT* context = get_context(phContext);
2471
0
  if (!context)
2472
0
    return SEC_E_INVALID_HANDLE;
2473
2474
0
  if (!(context->flags & SSPI_GSS_C_INTEG_FLAG))
2475
0
    return SEC_E_UNSUPPORTED_FUNCTION;
2476
2477
0
  sig_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_TOKEN);
2478
0
  data_buffer = sspi_FindSecBuffer(pMessage, SECBUFFER_DATA);
2479
2480
0
  if (!sig_buffer || !data_buffer || sig_buffer->cbBuffer < 16)
2481
0
    return SEC_E_INVALID_TOKEN;
2482
2483
  /* Read in header info */
2484
0
  BYTE* header = sig_buffer->pvBuffer;
2485
0
  tok_id = winpr_Data_Get_UINT16_BE(header);
2486
0
  flags = header[2];
2487
0
  seq_no = winpr_Data_Get_UINT64_BE((header + 8));
2488
2489
  /* Validate header */
2490
0
  if (tok_id != TOK_ID_MIC)
2491
0
    return SEC_E_INVALID_TOKEN;
2492
2493
0
  if ((flags & FLAG_SENDER_IS_ACCEPTOR) == context->acceptor || flags & FLAG_WRAP_CONFIDENTIAL)
2494
0
    return SEC_E_INVALID_TOKEN;
2495
2496
0
  if (memcmp(header + 3, cmp_filler, sizeof(cmp_filler)) != 0)
2497
0
    return SEC_E_INVALID_TOKEN;
2498
2499
0
  if (context->flags & ISC_REQ_SEQUENCE_DETECT && seq_no != context->remote_seq + MessageSeqNo)
2500
0
    return SEC_E_OUT_OF_SEQUENCE;
2501
2502
  /* Find the proper key and usage */
2503
0
  key = get_key(&context->keyset);
2504
0
  if (!key || (flags & FLAG_ACCEPTOR_SUBKEY && context->keyset.acceptor_key != key))
2505
0
    return SEC_E_INTERNAL_ERROR;
2506
0
  usage = context->acceptor ? KG_USAGE_INITIATOR_SIGN : KG_USAGE_ACCEPTOR_SIGN;
2507
2508
  /* Fill in the iov array lengths */
2509
0
  KRB_CREDENTIALS* creds = context->credentials;
2510
0
  iov[0].data.length = data_buffer->cbBuffer;
2511
0
  iov[1].data.length = 16;
2512
0
  if (krb_log_exec(krb5glue_crypto_length_iov, creds->ctx, key, iov, ARRAYSIZE(iov)))
2513
0
    return SEC_E_INTERNAL_ERROR;
2514
2515
0
  if (sig_buffer->cbBuffer != iov[2].data.length + 16)
2516
0
    return SEC_E_INTERNAL_ERROR;
2517
2518
  /* Set up the iov array */
2519
0
  iov[0].data.data = data_buffer->pvBuffer;
2520
0
  iov[1].data.data = (char*)header;
2521
0
  iov[2].data.data = (char*)&header[16];
2522
2523
0
  if (krb_log_exec(krb5glue_verify_checksum_iov, creds->ctx, key, usage, iov, ARRAYSIZE(iov),
2524
0
                   &is_valid))
2525
0
    return SEC_E_INTERNAL_ERROR;
2526
2527
0
  if (!is_valid)
2528
0
    return SEC_E_MESSAGE_ALTERED;
2529
2530
0
  return SEC_E_OK;
2531
#else
2532
  return SEC_E_UNSUPPORTED_FUNCTION;
2533
#endif
2534
0
}
2535
2536
const SecurityFunctionTableA KERBEROS_SecurityFunctionTableA = {
2537
  3,                                    /* dwVersion */
2538
  nullptr,                              /* EnumerateSecurityPackages */
2539
  kerberos_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
2540
  kerberos_AcquireCredentialsHandleA,   /* AcquireCredentialsHandle */
2541
  kerberos_FreeCredentialsHandle,       /* FreeCredentialsHandle */
2542
  nullptr,                              /* Reserved2 */
2543
  kerberos_InitializeSecurityContextA,  /* InitializeSecurityContext */
2544
  kerberos_AcceptSecurityContext,       /* AcceptSecurityContext */
2545
  nullptr,                              /* CompleteAuthToken */
2546
  kerberos_DeleteSecurityContext,       /* DeleteSecurityContext */
2547
  nullptr,                              /* ApplyControlToken */
2548
  kerberos_QueryContextAttributesA,     /* QueryContextAttributes */
2549
  nullptr,                              /* ImpersonateSecurityContext */
2550
  nullptr,                              /* RevertSecurityContext */
2551
  kerberos_MakeSignature,               /* MakeSignature */
2552
  kerberos_VerifySignature,             /* VerifySignature */
2553
  nullptr,                              /* FreeContextBuffer */
2554
  nullptr,                              /* QuerySecurityPackageInfo */
2555
  nullptr,                              /* Reserved3 */
2556
  nullptr,                              /* Reserved4 */
2557
  nullptr,                              /* ExportSecurityContext */
2558
  nullptr,                              /* ImportSecurityContext */
2559
  nullptr,                              /* AddCredentials */
2560
  nullptr,                              /* Reserved8 */
2561
  nullptr,                              /* QuerySecurityContextToken */
2562
  kerberos_EncryptMessage,              /* EncryptMessage */
2563
  kerberos_DecryptMessage,              /* DecryptMessage */
2564
  kerberos_SetContextAttributesA,       /* SetContextAttributes */
2565
  kerberos_SetCredentialsAttributesA,   /* SetCredentialsAttributes */
2566
};
2567
2568
const SecurityFunctionTableW KERBEROS_SecurityFunctionTableW = {
2569
  3,                                    /* dwVersion */
2570
  nullptr,                              /* EnumerateSecurityPackages */
2571
  kerberos_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
2572
  kerberos_AcquireCredentialsHandleW,   /* AcquireCredentialsHandle */
2573
  kerberos_FreeCredentialsHandle,       /* FreeCredentialsHandle */
2574
  nullptr,                              /* Reserved2 */
2575
  kerberos_InitializeSecurityContextW,  /* InitializeSecurityContext */
2576
  kerberos_AcceptSecurityContext,       /* AcceptSecurityContext */
2577
  nullptr,                              /* CompleteAuthToken */
2578
  kerberos_DeleteSecurityContext,       /* DeleteSecurityContext */
2579
  nullptr,                              /* ApplyControlToken */
2580
  kerberos_QueryContextAttributesW,     /* QueryContextAttributes */
2581
  nullptr,                              /* ImpersonateSecurityContext */
2582
  nullptr,                              /* RevertSecurityContext */
2583
  kerberos_MakeSignature,               /* MakeSignature */
2584
  kerberos_VerifySignature,             /* VerifySignature */
2585
  nullptr,                              /* FreeContextBuffer */
2586
  nullptr,                              /* QuerySecurityPackageInfo */
2587
  nullptr,                              /* Reserved3 */
2588
  nullptr,                              /* Reserved4 */
2589
  nullptr,                              /* ExportSecurityContext */
2590
  nullptr,                              /* ImportSecurityContext */
2591
  nullptr,                              /* AddCredentials */
2592
  nullptr,                              /* Reserved8 */
2593
  nullptr,                              /* QuerySecurityContextToken */
2594
  kerberos_EncryptMessage,              /* EncryptMessage */
2595
  kerberos_DecryptMessage,              /* DecryptMessage */
2596
  kerberos_SetContextAttributesW,       /* SetContextAttributes */
2597
  kerberos_SetCredentialsAttributesW,   /* SetCredentialsAttributes */
2598
};
2599
2600
BOOL KERBEROS_init(void)
2601
1
{
2602
1
  InitializeConstWCharFromUtf8(KERBEROS_SecPkgInfoA.Name, KERBEROS_SecPkgInfoW_NameBuffer,
2603
1
                               ARRAYSIZE(KERBEROS_SecPkgInfoW_NameBuffer));
2604
1
  InitializeConstWCharFromUtf8(KERBEROS_SecPkgInfoA.Comment, KERBEROS_SecPkgInfoW_CommentBuffer,
2605
1
                               ARRAYSIZE(KERBEROS_SecPkgInfoW_CommentBuffer));
2606
1
  return TRUE;
2607
1
}