Coverage Report

Created: 2024-05-20 06:11

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