Coverage Report

Created: 2023-09-25 06:56

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