jdk/src/windows/native/sun/security/krb5/NativeCreds.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 73 cf334423502b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 2000 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#define UNICODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#define _UNICODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#define SECURITY_WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <security.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <ntsecapi.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <dsgetdc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <lmcons.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <lmapibuf.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <winsock.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#undef LSA_SUCCESS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define LSA_SUCCESS(Status) ((Status) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define EXIT_FAILURE -1 // mdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Library-wide static references
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
jclass derValueClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
jclass ticketClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
jclass principalNameClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
jclass encryptionKeyClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
jclass ticketFlagsClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
jclass kerberosTimeClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
jclass javaLangStringClass = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
jmethodID derValueConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
jmethodID ticketConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
jmethodID principalNameConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
jmethodID encryptionKeyConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
jmethodID ticketFlagsConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
jmethodID kerberosTimeConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
jmethodID krbcredsConstructor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
jmethodID setRealmMethod = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Function prototypes for internal routines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
BOOL PackageConnectLookup(PHANDLE,PULONG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
NTSTATUS ConstructTicketRequest(UNICODE_STRING DomainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                PKERB_RETRIEVE_TKT_REQUEST *outRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                ULONG *outSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
DWORD ConcatenateUnicodeStrings(UNICODE_STRING *pTarget,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                UNICODE_STRING Source1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                UNICODE_STRING Source2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
VOID ShowNTError(LPSTR,NTSTATUS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
VOID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
InitUnicodeString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        PUNICODE_STRING DestinationString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    PCWSTR SourceString OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
jobject BuildTicket(JNIEnv *env, PUCHAR encodedTicket, ULONG encodedTicketSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
//mdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
jobject BuildPrincipal(JNIEnv *env, PKERB_EXTERNAL_NAME principalName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                UNICODE_STRING domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
jobject BuildEncryptionKey(JNIEnv *env, PKERB_CRYPTO_KEY cryptoKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
jobject BuildTicketFlags(JNIEnv *env, PULONG flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
jobject BuildKerberosTime(JNIEnv *env, PLARGE_INTEGER kerbtime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Class:     sun_security_krb5_KrbCreds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Method:    JNI_OnLoad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
JNIEXPORT jint JNICALL JNI_OnLoad(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                JavaVM  *jvm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                void    *reserved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        JNIEnv *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return JNI_EVERSION; /* JNI version not supported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        cls = (*env)->FindClass(env,"sun/security/krb5/internal/Ticket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                printf("Couldn't find Ticket\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        printf("Found Ticket\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ticketClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (ticketClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        cls = (*env)->FindClass(env, "sun/security/krb5/PrincipalName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                printf("Couldn't find PrincipalName\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        printf("Found PrincipalName\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        principalNameClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (principalNameClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        cls = (*env)->FindClass(env,"sun/security/util/DerValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                printf("Couldn't find DerValue\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        printf("Found DerValue\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        derValueClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (derValueClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        cls = (*env)->FindClass(env,"sun/security/krb5/EncryptionKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                printf("Couldn't find EncryptionKey\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        printf("Found EncryptionKey\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        encryptionKeyClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (encryptionKeyClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        cls = (*env)->FindClass(env,"sun/security/krb5/internal/TicketFlags");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                printf("Couldn't find TicketFlags\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        printf("Found TicketFlags\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        ticketFlagsClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (ticketFlagsClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        cls = (*env)->FindClass(env,"sun/security/krb5/internal/KerberosTime");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                printf("Couldn't find KerberosTime\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        printf("Found KerberosTime\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        kerberosTimeClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (kerberosTimeClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        cls = (*env)->FindClass(env,"java/lang/String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                printf("Couldn't find String\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        printf("Found String\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        javaLangStringClass = (*env)->NewWeakGlobalRef(env,cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (javaLangStringClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        printf("Made NewWeakGlobalRef\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        derValueConstructor = (*env)->GetMethodID(env, derValueClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                "<init>", "([B)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (derValueConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                printf("Couldn't find DerValue constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        printf("Found DerValue constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        ticketConstructor = (*env)->GetMethodID(env, ticketClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                "<init>", "(Lsun/security/util/DerValue;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (ticketConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                printf("Couldn't find Ticket constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        printf("Found Ticket constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        principalNameConstructor = (*env)->GetMethodID(env, principalNameClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                        "<init>", "([Ljava/lang/String;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (principalNameConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                printf("Couldn't find PrincipalName constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        printf("Found PrincipalName constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        encryptionKeyConstructor = (*env)->GetMethodID(env, encryptionKeyClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                "<init>", "(I[B)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (encryptionKeyConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                printf("Couldn't find EncryptionKey constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        printf("Found EncryptionKey constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        ticketFlagsConstructor = (*env)->GetMethodID(env, ticketFlagsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                                "<init>", "(I[B)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (ticketFlagsConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                printf("Couldn't find TicketFlags constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        printf("Found TicketFlags constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        kerberosTimeConstructor = (*env)->GetMethodID(env, kerberosTimeClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                        "<init>", "(Ljava/lang/String;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (kerberosTimeConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                printf("Couldn't find KerberosTime constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        printf("Found KerberosTime constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // load the setRealm method in PrincipalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        setRealmMethod = (*env)->GetMethodID(env, principalNameClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                        "setRealm", "(Ljava/lang/String;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (setRealmMethod == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                printf("Couldn't find setRealm in PrincipalName\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        printf("Finished OnLoad processing\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return JNI_VERSION_1_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * Class:     sun_security_jgss_KrbCreds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Method:    JNI_OnUnload
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
JNIEXPORT void JNICALL JNI_OnUnload(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                JavaVM  *jvm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                void    *reserved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        JNIEnv *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return; /* Nothing else we can do */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (ticketClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                (*env)->DeleteWeakGlobalRef(env,ticketClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (derValueClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                (*env)->DeleteWeakGlobalRef(env,derValueClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (principalNameClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                (*env)->DeleteWeakGlobalRef(env,principalNameClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (encryptionKeyClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                (*env)->DeleteWeakGlobalRef(env,encryptionKeyClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (ticketFlagsClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                (*env)->DeleteWeakGlobalRef(env,ticketFlagsClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (kerberosTimeClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                (*env)->DeleteWeakGlobalRef(env,kerberosTimeClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (javaLangStringClass != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                (*env)->DeleteWeakGlobalRef(env,javaLangStringClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 * Class:     sun_security_krb5_Credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 * Method:    acquireDefaultNativeCreds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 * Signature: ()Lsun/security/krb5/Credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
JNIEXPORT jobject JNICALL Java_sun_security_krb5_Credentials_acquireDefaultNativeCreds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                jclass krbcredsClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        KERB_QUERY_TKT_CACHE_REQUEST CacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        PKERB_RETRIEVE_TKT_RESPONSE TktCacheResponse = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        PKERB_RETRIEVE_TKT_REQUEST pTicketRequest = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        PKERB_RETRIEVE_TKT_RESPONSE pTicketResponse = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        NTSTATUS Status, SubStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        ULONG requestSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        ULONG responseSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        ULONG rspSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        HANDLE LogonHandle = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        ULONG PackageId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        jobject ticket, clientPrincipal, targetPrincipal, encryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        jobject ticketFlags, startTime, endTime, krbCreds = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        jobject authTime, renewTillTime, hostAddresses = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        KERB_EXTERNAL_TICKET *msticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        int ignore_cache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        FILETIME Now, EndTime, LocalEndTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        while (TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (krbcredsConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                krbcredsConstructor = (*env)->GetMethodID(env, krbcredsClass, "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        "(Lsun/security/krb5/internal/Ticket;Lsun/security/krb5/PrincipalName;Lsun/security/krb5/PrincipalName;Lsun/security/krb5/EncryptionKey;Lsun/security/krb5/internal/TicketFlags;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/HostAddresses;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (krbcredsConstructor == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                printf("Couldn't find sun.security.krb5.Credentials constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        printf("Found KrbCreds constructor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // Get the logon handle and package ID from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // Kerberos package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (!PackageConnectLookup(&LogonHandle, &PackageId))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        printf("Got handle to Kerberos package\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // Get the MS TGT from cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        CacheRequest.MessageType = KerbRetrieveTicketMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        CacheRequest.LogonId.LowPart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        CacheRequest.LogonId.HighPart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        Status = LsaCallAuthenticationPackage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        LogonHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        PackageId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        &CacheRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        sizeof(CacheRequest),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        &TktCacheResponse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        &rspSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        &SubStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        printf("Response size is %d\n", rspSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (!LSA_SUCCESS(Status) || !LSA_SUCCESS(SubStatus)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (!LSA_SUCCESS(Status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                ShowNTError("LsaCallAuthenticationPackage", Status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                ShowNTError("Protocol status", SubStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        // got the native MS TGT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        msticket = &(TktCacheResponse->Ticket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // check TGT validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        switch (msticket->SessionKey.KeyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            case KERB_ETYPE_DES_CBC_CRC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            case KERB_ETYPE_DES_CBC_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            case KERB_ETYPE_NULL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            case KERB_ETYPE_RC4_HMAC_NT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                GetSystemTimeAsFileTime(&Now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                EndTime.dwLowDateTime = msticket->EndTime.LowPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                EndTime.dwHighDateTime = msticket->EndTime.HighPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                FileTimeToLocalFileTime(&EndTime, &LocalEndTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                if (CompareFileTime(&Now, &LocalEndTime) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    ignore_cache = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (msticket->TicketFlags & KERB_TICKET_FLAGS_invalid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    ignore_cache = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            case KERB_ETYPE_RC4_MD4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                // not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                ignore_cache = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (ignore_cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            printf("MS TGT in cache is invalid/not supported; request new ticket\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // use domain to request Ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            Status = ConstructTicketRequest(msticket->TargetDomainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                &pTicketRequest, &requestSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (!LSA_SUCCESS(Status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                ShowNTError("ConstructTicketRequest status", Status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            pTicketRequest->MessageType = KerbRetrieveEncodedTicketMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            pTicketRequest->EncryptionType = KERB_ETYPE_DES_CBC_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            pTicketRequest->CacheOptions = KERB_RETRIEVE_TICKET_DONT_USE_CACHE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            Status = LsaCallAuthenticationPackage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        LogonHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        PackageId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        pTicketRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        requestSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        &pTicketResponse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        &responseSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        &SubStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            printf("Response size is %d\n", responseSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (!LSA_SUCCESS(Status) || !LSA_SUCCESS(SubStatus)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                if (!LSA_SUCCESS(Status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    ShowNTError("LsaCallAuthenticationPackage", Status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    ShowNTError("Protocol status", SubStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            // got the native MS Kerberos TGT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            msticket = &(pTicketResponse->Ticket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
typedef struct _KERB_RETRIEVE_TKT_RESPONSE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    KERB_EXTERNAL_TICKET Ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
} KERB_RETRIEVE_TKT_RESPONSE, *PKERB_RETRIEVE_TKT_RESPONSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
typedef struct _KERB_EXTERNAL_TICKET {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    PKERB_EXTERNAL_NAME ServiceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    PKERB_EXTERNAL_NAME TargetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    PKERB_EXTERNAL_NAME ClientName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    UNICODE_STRING DomainName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    UNICODE_STRING TargetDomainName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    UNICODE_STRING AltTargetDomainName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    KERB_CRYPTO_KEY SessionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    ULONG TicketFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    ULONG Flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    LARGE_INTEGER KeyExpirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    LARGE_INTEGER StartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    LARGE_INTEGER EndTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    LARGE_INTEGER RenewUntil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    LARGE_INTEGER TimeSkew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    ULONG EncodedTicketSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    PUCHAR EncodedTicket; <========== Here's the good stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
} KERB_EXTERNAL_TICKET, *PKERB_EXTERNAL_TICKET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
typedef struct _KERB_EXTERNAL_NAME {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    SHORT NameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    USHORT NameCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    UNICODE_STRING Names[ANYSIZE_ARRAY];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
} KERB_EXTERNAL_NAME, *PKERB_EXTERNAL_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
typedef struct _LSA_UNICODE_STRING {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    USHORT Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    USHORT MaximumLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    PWSTR  Buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
} LSA_UNICODE_STRING, *PLSA_UNICODE_STRING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
typedef LSA_UNICODE_STRING UNICODE_STRING, *PUNICODE_STRING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
typedef struct KERB_CRYPTO_KEY {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    LONG KeyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    ULONG Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    PUCHAR Value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
} KERB_CRYPTO_KEY, *PKERB_CRYPTO_KEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        // Build a com.sun.security.krb5.Ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        ticket = BuildTicket(env, msticket->EncodedTicket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                msticket->EncodedTicketSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (ticket == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // OK, have a Ticket, now need to get the client name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        clientPrincipal = BuildPrincipal(env, msticket->ClientName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                msticket->TargetDomainName); // mdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (clientPrincipal == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // and the "name" of tgt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        targetPrincipal = BuildPrincipal(env, msticket->ServiceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        msticket->DomainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (targetPrincipal == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // Get the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        encryptionKey = BuildEncryptionKey(env, &(msticket->SessionKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (encryptionKey == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // and the ticket flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        ticketFlags = BuildTicketFlags(env, &(msticket->TicketFlags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (ticketFlags == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // Get the start time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        startTime = BuildKerberosTime(env, &(msticket->StartTime));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (startTime == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * mdu: No point storing the eky expiration time in the auth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * time field. Set it to be same as startTime. Looks like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         * windows does not have post-dated tickets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        authTime = startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        // and the end time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        endTime = BuildKerberosTime(env, &(msticket->EndTime));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (endTime == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        // Get the renew till time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        renewTillTime = BuildKerberosTime(env, &(msticket->RenewUntil));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (renewTillTime == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        // and now go build a KrbCreds object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        krbCreds = (*env)->NewObject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                krbcredsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                krbcredsConstructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                clientPrincipal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                targetPrincipal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                encryptionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                ticketFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                authTime, // mdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                renewTillTime, //mdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                hostAddresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        } // end of WHILE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // clean up resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (TktCacheResponse != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                LsaFreeReturnBuffer(TktCacheResponse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (pTicketRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                LocalFree(pTicketRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (pTicketResponse != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                LsaFreeReturnBuffer(pTicketResponse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return krbCreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
static NTSTATUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
ConstructTicketRequest(UNICODE_STRING DomainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                PKERB_RETRIEVE_TKT_REQUEST *outRequest, ULONG *outSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        NTSTATUS Status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        UNICODE_STRING TargetPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        USHORT TargetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        ULONG RequestSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        ULONG Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        PKERB_RETRIEVE_TKT_REQUEST pTicketRequest = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        *outRequest = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        *outSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // Set up the "krbtgt/" target prefix into a UNICODE_STRING so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // can easily concatenate it later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        TargetPrefix.Buffer = L"krbtgt/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        Length = (ULONG)wcslen(TargetPrefix.Buffer) * sizeof(WCHAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        TargetPrefix.Length = (USHORT)Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        TargetPrefix.MaximumLength = TargetPrefix.Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        // We will need to concatenate the "krbtgt/" prefix and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // Logon Session's DnsDomainName into our request's target name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        // Therefore, first compute the necessary buffer size for that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        // Note that we might theoretically have integer overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        TargetSize = TargetPrefix.Length + DomainName.Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        // The ticket request buffer needs to be a single buffer.  That buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // needs to include the buffer for the target name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        RequestSize = sizeof (*pTicketRequest) + TargetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        // Allocate the request buffer and make sure it's zero-filled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        pTicketRequest = (PKERB_RETRIEVE_TKT_REQUEST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        LocalAlloc(LMEM_ZEROINIT, RequestSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (!pTicketRequest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Concatenate the target prefix with the previous reponse's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // target domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        pTicketRequest->TargetName.Length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        pTicketRequest->TargetName.MaximumLength = TargetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        pTicketRequest->TargetName.Buffer = (PWSTR) (pTicketRequest + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        Status = ConcatenateUnicodeStrings(&(pTicketRequest->TargetName),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                        TargetPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                        DomainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        *outRequest = pTicketRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        *outSize    = RequestSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return Status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
DWORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
ConcatenateUnicodeStrings(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    UNICODE_STRING *pTarget,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    UNICODE_STRING Source1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    UNICODE_STRING Source2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // The buffers for Source1 and Source2 cannot overlap pTarget's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        // buffer.  Source1.Length + Source2.Length must be <= 0xFFFF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        // otherwise we overflow...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        USHORT TotalSize = Source1.Length + Source2.Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        PBYTE buffer = (PBYTE) pTarget->Buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (TotalSize > pTarget->MaximumLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return ERROR_INSUFFICIENT_BUFFER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        pTarget->Length = TotalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        memcpy(buffer, Source1.Buffer, Source1.Length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        memcpy(buffer + Source1.Length, Source2.Buffer, Source2.Length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        return ERROR_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
BOOL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
PackageConnectLookup(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    HANDLE *pLogonHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    ULONG *pPackageId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    LSA_STRING Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    NTSTATUS Status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    Status = LsaConnectUntrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                pLogonHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    if (!LSA_SUCCESS(Status))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        ShowNTError("LsaConnectUntrusted", Status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    Name.Buffer = MICROSOFT_KERBEROS_NAME_A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    Name.Length = (USHORT)strlen(Name.Buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    Name.MaximumLength = Name.Length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    Status = LsaLookupAuthenticationPackage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                *pLogonHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                &Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                pPackageId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    if (!LSA_SUCCESS(Status))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        ShowNTError("LsaLookupAuthenticationPackage", Status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
VOID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
ShowLastError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        LPSTR szAPI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        DWORD dwError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
   #define MAX_MSG_SIZE 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
   static WCHAR szMsgBuf[MAX_MSG_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
   DWORD dwRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
   printf("Error calling function %s: %lu\n", szAPI, dwError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
   dwRes = FormatMessage (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
      FORMAT_MESSAGE_FROM_SYSTEM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
      NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
      dwError,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
      0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
      szMsgBuf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
      MAX_MSG_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
      NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
   if (0 == dwRes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
      printf("FormatMessage failed with %d\n", GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
      // ExitProcess(EXIT_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
      printf("%S",szMsgBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
VOID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
ShowNTError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        LPSTR szAPI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        NTSTATUS Status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    // Convert the NTSTATUS to Winerror. Then call ShowLastError().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    ShowLastError(szAPI, LsaNtStatusToWinError(Status));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
VOID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
InitUnicodeString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        PUNICODE_STRING DestinationString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    PCWSTR SourceString OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    ULONG Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    DestinationString->Buffer = (PWSTR)SourceString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    if (SourceString != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        Length = (ULONG)wcslen( SourceString ) * sizeof( WCHAR );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        DestinationString->Length = (USHORT)Length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        DestinationString->MaximumLength = (USHORT)(Length + sizeof(UNICODE_NULL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        DestinationString->MaximumLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        DestinationString->Length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
jobject BuildTicket(JNIEnv *env, PUCHAR encodedTicket, ULONG encodedTicketSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        /* To build a Ticket, we first need to build a DerValue out of the EncodedTicket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
         * But before we can do that, we need to make a byte array out of the ET.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        jobject derValue, ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        jbyteArray ary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        ary = (*env)->NewByteArray(env,encodedTicketSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                return (jobject) NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        (*env)->SetByteArrayRegion(env, ary, (jsize) 0, encodedTicketSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                        (jbyte *)encodedTicket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                (*env)->DeleteLocalRef(env, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                return (jobject) NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        derValue = (*env)->NewObject(env, derValueClass, derValueConstructor, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                (*env)->DeleteLocalRef(env, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                return (jobject) NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        (*env)->DeleteLocalRef(env, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        ticket = (*env)->NewObject(env, ticketClass, ticketConstructor, derValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                (*env)->DeleteLocalRef(env, derValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                return (jobject) NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        (*env)->DeleteLocalRef(env, derValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
// mdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
jobject BuildPrincipal(JNIEnv *env, PKERB_EXTERNAL_NAME principalName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                                UNICODE_STRING domainName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         * To build the Principal, we need to get the names out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * this goofy MS structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        jobject principal = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        jobject realmStr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        jobjectArray stringArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        jstring tempString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        int nameCount,i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        PUNICODE_STRING scanner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        WCHAR *realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        ULONG realmLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        realm = (WCHAR *) LocalAlloc(LMEM_ZEROINIT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                ((domainName.Length)*sizeof(WCHAR) + sizeof(UNICODE_NULL)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        wcsncpy(realm, domainName.Buffer, domainName.Length/sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        printf("Principal domain is %S\n", realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        printf("Name type is %x\n", principalName->NameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        printf("Name count is %x\n", principalName->NameCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        nameCount = principalName->NameCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        stringArray = (*env)->NewObjectArray(env, nameCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                                javaLangStringClass, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (stringArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            printf("Can't allocate String array for Principal\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            LocalFree(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        for (i=0; i<nameCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            // get the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            scanner = &(principalName->Names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            // OK, got a Char array, so construct a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            tempString = (*env)->NewString(env, (const jchar*)scanner->Buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                                scanner->Length/sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            // Set the String into the StringArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            (*env)->SetObjectArrayElement(env, stringArray, i, tempString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            // Do I have to worry about storage reclamation here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        principal = (*env)->NewObject(env, principalNameClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                        principalNameConstructor, stringArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        // now set the realm in the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        realmLen = (ULONG)wcslen((PWCHAR)realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        realmStr = (*env)->NewString(env, (PWCHAR)realm, (USHORT)realmLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        (*env)->CallVoidMethod(env, principal, setRealmMethod, realmStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        // free local resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        LocalFree(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
jobject BuildEncryptionKey(JNIEnv *env, PKERB_CRYPTO_KEY cryptoKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        // First, need to build a byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        jbyteArray ary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        jobject encryptionKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        ary = (*env)->NewByteArray(env,cryptoKey->Length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        (*env)->SetByteArrayRegion(env, ary, (jsize) 0, cryptoKey->Length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                        (jbyte *)cryptoKey->Value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                (*env)->DeleteLocalRef(env, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                encryptionKey = (*env)->NewObject(env, encryptionKeyClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        encryptionKeyConstructor, cryptoKey->KeyType, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        return encryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
jobject BuildTicketFlags(JNIEnv *env, PULONG flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        jobject ticketFlags = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        jbyteArray ary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * mdu: Convert the bytes to nework byte order before copying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * them to a Java byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        ULONG nlflags = htonl(*flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        ary = (*env)->NewByteArray(env, sizeof(*flags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        (*env)->SetByteArrayRegion(env, ary, (jsize) 0, sizeof(*flags),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                        (jbyte *)&nlflags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                (*env)->DeleteLocalRef(env, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                ticketFlags = (*env)->NewObject(env, ticketFlagsClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                        ticketFlagsConstructor, sizeof(*flags)*8, ary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        return ticketFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
jobject BuildKerberosTime(JNIEnv *env, PLARGE_INTEGER kerbtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        jobject kerberosTime = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        jstring stringTime = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        SYSTEMTIME systemTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        WCHAR timeString[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        WCHAR month[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        WCHAR day[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        WCHAR hour[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        WCHAR minute[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        WCHAR second[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (FileTimeToSystemTime((FILETIME *)kerbtime, &systemTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
// XXX Cannot use %02.2ld, because the leading 0 is ignored for integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
// So, print them to strings, and then print them to the master string with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
// format pattern that makes it two digits and prefix with a 0 if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                swprintf( (wchar_t *)month, L"%2.2d", systemTime.wMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                swprintf( (wchar_t *)day, L"%2.2d", systemTime.wDay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                swprintf( (wchar_t *)hour, L"%2.2d", systemTime.wHour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                swprintf( (wchar_t *)minute, L"%2.2d", systemTime.wMinute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                swprintf( (wchar_t *)second, L"%2.2d", systemTime.wSecond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                swprintf( (wchar_t *)timeString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                                L"%ld%02.2s%02.2s%02.2s%02.2s%02.2sZ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                systemTime.wYear,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                month,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                day,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                hour,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                minute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                second );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                printf("%S\n", (wchar_t *)timeString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                #endif /* DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                stringTime = (*env)->NewString(env, timeString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                                (sizeof(timeString)/sizeof(WCHAR))-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                if (stringTime != NULL) { // everything's OK so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                        kerberosTime = (*env)->NewObject(env, kerberosTimeClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                                kerberosTimeConstructor, stringTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        return kerberosTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
}