Coverage Report

Created: 2026-01-16 07:10

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