summaryrefslogtreecommitdiffstats
path: root/private/net/svcdlls/ntlmssp/common/credhand.c
blob: 19ffcba1813b120483514b6903a181b86e491268 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
/*++

Copyright (c) 1993  Microsoft Corporation

Module Name:

    credhand.c

Abstract:

    API and support routines for handling credential handles.

Author:

    Cliff Van Dyke (CliffV) 26-Jun-1993

Revision History:

--*/


//
// Common include files.
//

#include <ntlmcomn.h>   // Common definitions for DLL and SERVICE
#include <ntlmsspi.h>   // Data private to the common routines
#include <align.h>      // ALIGN_WHCAR

//
// Crit Sect to protect various globals in this module.
//

CRITICAL_SECTION SspCredentialCritSect;



LIST_ENTRY SspCredentialList;



SECURITY_STATUS
SspGetUnicodeStringFromClient(
    IN PSSP_CLIENT_CONNECTION ClientConnection,
    IN LPWSTR String,
    IN ULONG StringSize,
    IN ULONG MaximumLength,
    OUT PUNICODE_STRING OutputString
    )

/*++

Routine Description:

    This routine copies the InputMessage into the local address space.
    This routine then validates the message header.

Arguments:

    ClientConnection - Describes the client process.

    String - Address of the string in the client process (must include
        trailing zero character).

    StringSize - Size of the string (in bytes).

    MaximumLength - Maximum length of the string (in characters) (not including
        the trailing zero characer).

    OutputString - Returns a UNICODE_STRING with an allocated buffer that
        contains the string.  The buffer should be freed using LocalFree.
        The field will be set NULL if the input string is NULL.

Return Value:

    STATUS_SUCCESS - Call completed successfully

    SEC_E_INVALID_TOKEN -- Message improperly formatted
    SEC_E_UNKNOWN_CREDENTIALS -- Credentials are improperly formed

--*/

{
    SECURITY_STATUS SecStatus;
    LPWSTR AllocatedString;


    //
    // If the caller didn't pass a string,
    //  just indicate so.
    //

    if ( String == NULL && StringSize == 0 ) {
        RtlInitUnicodeString(
            OutputString,
            NULL
            );
        return STATUS_SUCCESS;
    }

    //
    // Allocate a local buffer for the message.
    //

    if ( !COUNT_IS_ALIGNED(StringSize, ALIGN_WCHAR) ||
         StringSize > (MaximumLength+1) * sizeof(WCHAR) ) {
        return SEC_E_UNKNOWN_CREDENTIALS;
    }

    AllocatedString = LocalAlloc( 0, StringSize );

    if ( AllocatedString == NULL ) {
        return SEC_E_INSUFFICIENT_MEMORY;
    }


    //
    // Copy the message into the buffer
    //

    SecStatus = SspLpcCopyFromClientBuffer (
                    ClientConnection,
                    StringSize,
                    AllocatedString,
                    String );

    if ( !NT_SUCCESS(SecStatus) ) {
        (VOID) LocalFree( AllocatedString );
        return SecStatus;
    }


    //
    // Ensure the string is trailing zero terminated.
    //

    if ( AllocatedString[(StringSize/sizeof(WCHAR))-1] != L'\0' ) {
        (VOID) LocalFree( AllocatedString );
        return SEC_E_UNKNOWN_CREDENTIALS;
    }

    OutputString->Buffer = AllocatedString;
    OutputString->MaximumLength = (USHORT) StringSize;
    OutputString->Length = (USHORT) (StringSize - sizeof(WCHAR));

    return STATUS_SUCCESS;
}




PSSP_CREDENTIAL
SspCredentialReferenceCredential(
    IN PCredHandle CredentialHandle,
    IN PSSP_CLIENT_CONNECTION ClientConnection,
    IN BOOLEAN DereferenceCredential,
    IN BOOLEAN ForceRemoveCredential
    )

/*++

Routine Description:

    This routine checks to see if the Credential is from a currently
    active client, and references the Credential if it is valid.

    The caller may optionally request that the client's Credential be
    removed from the list of valid Credentials - preventing future
    requests from finding this Credential.

    For a client's Credential to be valid, the Credential value
    must be on our list of active Credentials.


Arguments:

    CredentialHandle - Points to the CredentialHandle of the Credential
        to be referenced.

    ClientConnection - Points to the client connection of the client
        referencing the handle.  (NULL means an internal reference.)

    DereferenceCredential - This boolean value indicates that that a call
        a single instance of this credential handle should be freed. If there
        are multiple instances, they should still continue to work.

    ForceRemoveCredential - This boolean value indicates whether the caller
        wants the logon process's Credential to be removed from the list
        of Credentials.  TRUE indicates the Credential is to be removed.
        FALSE indicates the Credential is not to be removed.


Return Value:

    NULL - the Credential was not found.

    Otherwise - returns a pointer to the referenced credential.

--*/

{
    PLIST_ENTRY ListEntry;
    PSSP_CREDENTIAL Credential;

    //
    // Sanity check
    //

    if ( CredentialHandle->dwLower != SspCommonSecHandleValue ) {
        return NULL;
    }

    //
    // Make sure that nobody tries to force removal without also
    // trying to dereference the credential.
    //

    ASSERT(!(ForceRemoveCredential && !DereferenceCredential));

    //
    // Acquire exclusive access to the Credential list
    //

    EnterCriticalSection( &SspCredentialCritSect );


    //
    // Now walk the list of Credentials looking for a match.
    //

    for ( ListEntry = SspCredentialList.Flink;
          ListEntry != &SspCredentialList;
          ListEntry = ListEntry->Flink ) {

        Credential = CONTAINING_RECORD( ListEntry, SSP_CREDENTIAL, Next );


        //
        // Found a match ... reference this Credential
        // (if the Credential is being removed, we would increment
        // and then decrement the reference, so don't bother doing
        // either - since they cancel each other out).
        //

        if ( Credential == (PSSP_CREDENTIAL) CredentialHandle->dwUpper &&
            (ClientConnection == NULL ||
            ClientConnection == Credential->ClientConnection )) {


            if (!DereferenceCredential) {
                Credential->References += 1;
            } else {

                //
                // Decremenent the credential references, indicating
                // that a call to free

                Credential->CredentialReferences--;

                if (ForceRemoveCredential || (Credential->CredentialReferences == 0)) {

                    RemoveEntryList( &Credential->Next );
                    RemoveEntryList( &Credential->NextForThisClient );
                    Credential->Unlinked = TRUE;

                    //
                    // If we are forcing removal, get rid of the appropriate
                    // number of references from all the other instances.
                    // This is used when the client connection is dropped.
                    //

                    if (ForceRemoveCredential) {
                        Credential->References -= Credential->CredentialReferences;
                    }

                    Credential->CredentialReferences = 0;

                    SspPrint(( SSP_API_MORE, "Delinked Credential 0x%lx\n",
                               Credential ));

                }
            }

            LeaveCriticalSection( &SspCredentialCritSect );
            return Credential;

        }

    }


    //
    // No match found
    //
    SspPrint(( SSP_API, "Tried to reference unknown Credential 0x%lx\n",
               CredentialHandle->dwUpper ));

    LeaveCriticalSection( &SspCredentialCritSect );
    return NULL;

}

SECURITY_STATUS
SspCredentialGetPassword(
    IN PSSP_CREDENTIAL Credential,
    OUT PUNICODE_STRING Password
    )
/*++

Routine Description:

    This routine copies the password out of credential. It requires locking
    the credential list because other threads may be hiding/revealing the
    password and we need exclusive access to do that.

Arguments:

    Credential - Credential record to retrieve the password from.

    Password - UNICODE_STRING to store the password in.


Return Value:

    SEC_E_INSUFFICIENT_MEMORY - there was not enough memory to copy
        the password.

--*/

{
    SECURITY_STATUS SecStatus = SEC_E_OK;
    EnterCriticalSection(&SspCredentialCritSect);

    SspRevealPassword(&Credential->Password);
    if ( Credential->Password.Buffer != NULL ) {
        SecStatus = SspDuplicateUnicodeString(
                        Password,
                        &Credential->Password
                        );
    } else {
        RtlInitUnicodeString(
            Password,
            NULL
            );
    }
    if (NT_SUCCESS(SecStatus)) {
        SspHidePassword(Password);
    }

    SspHidePassword(&Credential->Password);
    LeaveCriticalSection(&SspCredentialCritSect);
    return(SecStatus);
}


PSSP_CREDENTIAL
SspCredentialLookupSupplementalCredential(
    IN PSSP_CLIENT_CONNECTION ClientConnection,
    IN PLUID LogonId,
    IN PUNICODE_STRING UserName,
    IN PUNICODE_STRING DomainName,
    IN PUNICODE_STRING Password
    )

/*++

Routine Description:

    This routine walks the list of credentials for this client looking
    for one that has the same supplemental credentials as those passed
    in.  If it is found, its reference count is increased and a pointer
    to it is returned.


Arguments:

    ClientConnection - Points to the client connection of the client
        referencing the handle.

    UserName - User name to match.

    DomainName - Domain name to match.

    Password - Password to match.


Return Value:

    NULL - the Credential was not found.

    Otherwise - returns a pointer to the referenced credential.

--*/

{
    PLIST_ENTRY ListEntry;
    PSSP_CREDENTIAL Credential;
    PLIST_ENTRY ListHead;


    //
    // Acquire exclusive access to the Credential list
    //

    EnterCriticalSection( &SspCredentialCritSect );

    if (ClientConnection == NULL) {
        ListHead = &SspCredentialList;
    } else {
        ListHead = &ClientConnection->CredentialHead;
    }

    //
    // Now walk the list of Credentials looking for a match.
    //

    for ( ListEntry = ListHead->Flink;
          ListEntry != ListHead;
          ListEntry = ListEntry->Flink ) {

        if (ClientConnection != NULL) {
            Credential = CONTAINING_RECORD( ListEntry, SSP_CREDENTIAL, NextForThisClient );
        } else {
            Credential = CONTAINING_RECORD( ListEntry, SSP_CREDENTIAL, Next );
        }

        //
        // We are only looking for outbound credentials.
        //

        if ((Credential->CredentialUseFlags & SECPKG_CRED_OUTBOUND) == 0) {
            continue;
        }

        //
        // Check for a match
        //
        if ( RtlEqualUnicodeString(
                UserName,
                &Credential->UserName,
                FALSE
                ) &&
            RtlEqualUnicodeString(
                DomainName,
                &Credential->DomainName,
                FALSE
                ) &&
            RtlEqualLuid(
                LogonId,
                &Credential->LogonId
                )) {

            SspRevealPassword(&Credential->Password);

            if (RtlEqualUnicodeString(
                    Password,
                    &Credential->Password,
                    FALSE
                    )) {

                //
                // Found a match - reference the credential
                //

                SspHidePassword(&Credential->Password);

                //
                // Reference the credential and indicate that
                // it is in use as two different handles to the caller
                // (who may call FreeCredentialsHandle twice)
                //

                Credential->References++;
                Credential->CredentialReferences++;

                LeaveCriticalSection( &SspCredentialCritSect );
                return Credential;

            }
            SspHidePassword(&Credential->Password);


        }

    }


    //
    // No match found
    //
    SspPrint(( SSP_API, "Tried to reference unknown Credential\n" ));

    LeaveCriticalSection( &SspCredentialCritSect );
    return NULL;

}


VOID
SspCredentialDereferenceCredential(
    IN PSSP_CREDENTIAL Credential
    )

/*++

Routine Description:

    This routine decrements the specified Credential's reference count.
    If the reference count drops to zero, then the Credential is deleted

Arguments:

    Credential - Points to the Credential to be dereferenced.


Return Value:

    None.

--*/

{
    ULONG References;

    //
    // Decrement the reference count
    //

    EnterCriticalSection( &SspCredentialCritSect );
    ASSERT( Credential->References >= 1 );

    References = -- Credential->References;

    LeaveCriticalSection( &SspCredentialCritSect );

    //
    // If the count dropped to zero, then run-down the Credential
    //

    if ( References == 0) {

        SspPrint(( SSP_API_MORE, "Deleting Credential 0x%lx\n",
                   Credential ));

        if ( Credential->DomainName.Buffer != NULL ) {
            (VOID) LocalFree( Credential->DomainName.Buffer );
        }
        if ( Credential->UserName.Buffer != NULL ) {
            (VOID) LocalFree( Credential->UserName.Buffer );
        }
        if ( Credential->Password.Buffer != NULL ) {
            (VOID) LocalFree( Credential->Password.Buffer );
        }

        if (!Credential->Unlinked) {
            RemoveEntryList( &Credential->Next );
            RemoveEntryList( &Credential->NextForThisClient );
        }

        if (Credential->ClientTokenHandle != NULL) {
            (VOID) NtClose(Credential->ClientTokenHandle);
        }
        (VOID) LocalFree( Credential );

    }


    return;

}



VOID
SspCredentialClientConnectionDropped(
    PSSP_CLIENT_CONNECTION ClientConnection
    )

/*++

Routine Description:

    This routine is called when the ClientConnection is dropped to allow
    us to remove any Credentials for the ClientConnection.

Arguments:

    ClientConnection - Pointer to the ClientConnection that has been dropped.


Return Value:

    None.

--*/

{

    //
    // Drop any lingering Credentials
    //

    EnterCriticalSection( &SspCredentialCritSect );
    while ( !IsListEmpty( &ClientConnection->CredentialHead ) ) {
        CredHandle CredentialHandle;
        PSSP_CREDENTIAL Credential;

        CredentialHandle.dwUpper =
            (LONG) CONTAINING_RECORD( ClientConnection->CredentialHead.Flink,
                                      SSP_CREDENTIAL,
                                      NextForThisClient );

        CredentialHandle.dwLower = SspCommonSecHandleValue;

        LeaveCriticalSection( &SspCredentialCritSect );

        Credential = SspCredentialReferenceCredential(
                                &CredentialHandle,
                                ClientConnection,
                                TRUE,
                                TRUE);            // Remove Credential

        if ( Credential != NULL ) {
            SspCredentialDereferenceCredential(Credential);
        }

        EnterCriticalSection( &SspCredentialCritSect );
    }
    LeaveCriticalSection( &SspCredentialCritSect );

}



SECURITY_STATUS
SsprAcquireCredentialHandle(
    IN PSSP_CLIENT_CONNECTION ClientConnection,
    IN PHANDLE ClientTokenHandle,
    IN PLUID LogonId,
    IN ULONG CredentialUseFlags,
    OUT PCredHandle CredentialHandle,
    OUT PTimeStamp Lifetime,
    IN LPWSTR DomainName,
    IN ULONG DomainNameSize,
    IN LPWSTR UserName,
    IN ULONG UserNameSize,
    IN LPWSTR Password,
    IN ULONG PasswordSize
    )

/*++

Routine Description:

    This API allows applications to acquire a handle to pre-existing
    credentials associated with the user on whose behalf the call is made
    i.e. under the identity this application is running.  These pre-existing
    credentials have been established through a system logon not described
    here.  Note that this is different from "login to the network" and does
    not imply gathering of credentials.


    This API returns a handle to the credentials of a principal (user, client)
    as used by a specific security package.  This handle can then be used
    in subsequent calls to the Context APIs.  This API will not let a
    process obtain a handle to credentials that are not related to the
    process; i.e. we won't allow a process to grab the credentials of
    another user logged into the same machine.  There is no way for us
    to determine if a process is a trojan horse or not, if it is executed
    by the user.

Arguments:

    ClientConnection - Describes the client process.

    CredentialUseFlags - Flags indicating the way with which these
        credentials will be used.

        #define     CRED_INBOUND        0x00000001
        #define     CRED_OUTBOUND       0x00000002
        #define     CRED_BOTH           0x00000003

        The credentials created with CRED_INBOUND option can only be used
        for (validating incoming calls and can not be used for making accesses.

    CredentialHandle - Returned credential handle.

    Lifetime - Time that these credentials expire. The value returned in
        this field depends on the security package.

    DomainName, DomainNameSize, UserName, UserNameSize, Password, PasswordSize -
        Optional credentials for this user.

Return Value:

    STATUS_SUCCESS -- Call completed successfully

    SEC_E_PRINCIPAL_UNKNOWN -- No such principal
    SEC_E_NOT_OWNER -- caller does not own the specified credentials
    SEC_E_INSUFFICIENT_MEMORY -- Not enough memory

--*/

{
    SECURITY_STATUS SecStatus;
    NTSTATUS Status;
    PSSP_CREDENTIAL Credential = NULL;
    UNICODE_STRING LocalDomainName;
    UNICODE_STRING LocalUserName;
    UNICODE_STRING LocalPassword;
    TOKEN_STATISTICS TokenStatisticsInfo;
    ULONG TokenStatisticsInfoSize = sizeof(TOKEN_STATISTICS);

    //
    // Initialization
    //

    RtlInitUnicodeString(
        &LocalDomainName,
        NULL
        );

    RtlInitUnicodeString(
        &LocalUserName,
        NULL
        );

    RtlInitUnicodeString(
        &LocalPassword,
        NULL
        );

    SspPrint(( SSP_API, "SsprAcquireCredentialHandle Entered\n" ));


    //
    // Ensure at least one Credential use bit is set.
    //

    if ( (CredentialUseFlags & (SECPKG_CRED_INBOUND|SECPKG_CRED_OUTBOUND)) == 0 ) {
        SspPrint(( SSP_API,
            "SsprAcquireCredentialHandle: invalid credential use.\n" ));
        SecStatus = SEC_E_INVALID_CREDENTIAL_USE;
        goto Cleanup;
    }


    //
    // Copy the default credentials to the credential block
    //

    SecStatus = SspGetUnicodeStringFromClient(
                    ClientConnection,
                    DomainName,
                    DomainNameSize,
                    DNLEN,
                    &LocalDomainName );

    if ( !NT_SUCCESS(SecStatus) ) {
        SspPrint(( SSP_API, "Cannot copy domain name.\n" ));
        goto Cleanup;
    }

    SecStatus = SspGetUnicodeStringFromClient(
                    ClientConnection,
                    UserName,
                    UserNameSize,
                    UNLEN,
                    &LocalUserName );

    if ( !NT_SUCCESS(SecStatus) ) {
        SspPrint(( SSP_API, "Cannot copy user name.\n" ));
        goto Cleanup;
    }

    SecStatus = SspGetUnicodeStringFromClient(
                    ClientConnection,
                    Password,
                    PasswordSize,
                    PWLEN,
                    &LocalPassword );

    if ( !NT_SUCCESS(SecStatus) ) {
        SspPrint(( SSP_API, "Cannot copy password.\n" ));
        goto Cleanup;
    }


    Status = NtQueryInformationToken(
                *ClientTokenHandle,
                TokenStatistics,
                &TokenStatisticsInfo,
                TokenStatisticsInfoSize,
                &TokenStatisticsInfoSize );

    if (!NT_SUCCESS(Status)) {
        SecStatus = SspNtStatusToSecStatus( Status, SEC_E_NO_IMPERSONATION );
        goto Cleanup;
    }

    //
    // If this is an outbound credential, and supplemental credentials
    // were supplied, look to see if we have already
    // created one with this set of credentials. Note - this leaves
    // the credential referenced, so if we fail further down we need to
    // dereference the credential.
    //

    if ((CredentialUseFlags & SECPKG_CRED_OUTBOUND) != 0) {

        Credential = SspCredentialLookupSupplementalCredential(
                        ClientConnection,
                        &TokenStatisticsInfo.AuthenticationId,
                        &LocalUserName,
                        &LocalDomainName,
                        &LocalPassword
                        );



    }

    //
    // If we didn't just find a credential, create one now.
    //

    if (Credential == NULL) {

        //
        // Allocate a credential block and initialize it.
        //

        Credential = LocalAlloc( 0, sizeof(SSP_CREDENTIAL) );

        if ( Credential == NULL ) {
            SspPrint(( SSP_API, "Cannot allocate credential.\n" ));
            SecStatus = SEC_E_INSUFFICIENT_MEMORY;
            goto Cleanup;
        }

        //
        // Actually its on both a global credential list and a per client connection
        // list, but we link/delink from both lists at the same time so a single
        // reference count handles both.
        //

        Credential->References = 1;
        Credential->CredentialReferences = 1;
        Credential->ClientConnection = ClientConnection;
        Credential->CredentialUseFlags = CredentialUseFlags;
        Credential->Unlinked = FALSE;

        //
        // Stick the token and logon ID in the credential
        //

        Credential->ClientTokenHandle = *ClientTokenHandle,
        *ClientTokenHandle = NULL;
        Credential->LogonId = *LogonId;

        //
        // Stick the supplemental credentials into the credential.
        //

        Credential->UserName = LocalUserName;
        LocalUserName.Buffer = NULL;
        Credential->DomainName = LocalDomainName;
        LocalDomainName.Buffer = NULL;

        SspHidePassword(&LocalPassword);
        Credential->Password = LocalPassword;
        LocalPassword.Buffer = NULL;

        //
        // Add it to the list of valid credential handles.
        //

        EnterCriticalSection( &SspCredentialCritSect );
        InsertHeadList( &SspCredentialList, &Credential->Next );
        if ( ClientConnection != NULL ) {
            InsertHeadList( &ClientConnection->CredentialHead,
                            &Credential->NextForThisClient );
        } else {
            InitializeListHead( &Credential->NextForThisClient );
        }
        LeaveCriticalSection( &SspCredentialCritSect );

        SspPrint(( SSP_API_MORE, "Added Credential 0x%lx\n", Credential ));

        //
        // Don't bother dereferencing because we already set the
        // reference count to 1.
        //

    }

    //
    // Return output parameters to the caller.
    //

    CredentialHandle->dwUpper = (DWORD) Credential;
    CredentialHandle->dwLower = SspCommonSecHandleValue;
    *Lifetime = SspGlobalForever;

    SecStatus = STATUS_SUCCESS;

    //
    // Free and locally used resources.
    //

Cleanup:

    if ( !NT_SUCCESS(SecStatus) ) {

        if ( Credential != NULL ) {
            (VOID)LocalFree( Credential );
        }

    }
    if (LocalUserName.Buffer != NULL) {
        (VOID)LocalFree(LocalUserName.Buffer);
    }

    if (LocalDomainName.Buffer != NULL) {
        (VOID)LocalFree(LocalDomainName.Buffer);
    }

    if (LocalPassword.Buffer != NULL) {
        (VOID)LocalFree(LocalPassword.Buffer);
    }

    SspPrint(( SSP_API, "SspAcquireCredentialHandle returns 0x%lx\n", SecStatus ));

    return SecStatus;
}

SECURITY_STATUS
SsprFreeCredentialHandle(
    IN PSSP_CLIENT_CONNECTION ClientConnection,
    IN PCredHandle CredentialHandle
    )

/*++

Routine Description:

    This API is used to notify the security system that the credentials are
    no longer needed and allows the application to free the handle acquired
    in the call described above. When all references to this credential
    set has been removed then the credentials may themselves be removed.

Arguments:


    ClientConnection - Describes the client process.

    CredentialHandle - Credential Handle obtained through
        AcquireCredentialHandle.

Return Value:


    STATUS_SUCCESS -- Call completed successfully

    SEC_E_NO_SPM -- Security Support Provider is not running
    SEC_E_INVALID_HANDLE -- Credential Handle is invalid


--*/

{
    SECURITY_STATUS SecStatus;
    PSSP_CREDENTIAL Credential;

    //
    // Initialization
    //

    SspPrint(( SSP_API, "SspFreeCredentialHandle Entered\n" ));

    //
    // Find the referenced credential and delink it.
    //

    Credential = SspCredentialReferenceCredential(
                            CredentialHandle,
                            ClientConnection,
                            TRUE,       // remove the instance of the credential
                            FALSE);

    if ( Credential == NULL ) {
        SecStatus = SEC_E_INVALID_HANDLE;
        goto Cleanup;
    }

    //
    // Dereferencing the Credential will remove the client's reference
    // to it, causing it to be rundown if nobody else is using it.
    //

    SspCredentialDereferenceCredential( Credential );


    SecStatus = STATUS_SUCCESS;

    //
    // Free and locally used resources.
    //
Cleanup:

    SspPrint(( SSP_API, "SspFreeCredentialHandle returns 0x%lx\n", SecStatus ));
    return SecStatus;
}




NTSTATUS
SspCredentialInitialize(
    VOID
    )

/*++

Routine Description:

    This function initializes this module.

Arguments:

    None.

Return Value:

    Status of the operation.

--*/

{

    //
    // Initialize the Credential list to be empty.
    //

    InitializeCriticalSection(&SspCredentialCritSect);
    InitializeListHead( &SspCredentialList );

    return STATUS_SUCCESS;

}




VOID
SspCredentialTerminate(
    VOID
    )

/*++

Routine Description:

    This function cleans up any dangling credentials.

Arguments:

    None.

Return Value:

    Status of the operation.

--*/

{

    //
    // Drop any lingering Credentials
    //

    EnterCriticalSection( &SspCredentialCritSect );
    while ( !IsListEmpty( &SspCredentialList ) ) {
        CredHandle CredentialHandle;
        PSSP_CREDENTIAL Credential;

        CredentialHandle.dwUpper =
            (LONG) CONTAINING_RECORD( SspCredentialList.Flink,
                                      SSP_CREDENTIAL,
                                      Next );

        CredentialHandle.dwLower = SspCommonSecHandleValue;

        LeaveCriticalSection( &SspCredentialCritSect );

        Credential = SspCredentialReferenceCredential(
                                &CredentialHandle,
                                NULL,             // Don't know the Connection
                                TRUE,
                                TRUE);            // Remove Credential

        if ( Credential != NULL ) {
            SspCredentialDereferenceCredential(Credential);
        }

        EnterCriticalSection( &SspCredentialCritSect );
    }
    LeaveCriticalSection( &SspCredentialCritSect );


    //
    // Delete the critical section
    //

    DeleteCriticalSection(&SspCredentialCritSect);

    return;

}