Coverage Report

Created: 2026-04-12 06:58

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