src/java.security.jgss/macosx/native/libosxkrb5/nativeccache.c
author ihse
Tue, 13 Mar 2018 11:32:32 +0100
branchihse-jdk-library-branch
changeset 56283 871659d45aca
parent 47216 71c04702a3d5
child 57487 643978a35f6e
child 58678 9cf78a70fa4f
permissions -rw-r--r--
Create ihse-jdk-library-branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29491
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
#import "sun_security_krb5_Credentials.h"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
#import <Kerberos/Kerberos.h>
29491
821faf99fb06 8074836: Resolve disabled warnings for libosxkrb5
weijun
parents: 25859
diff changeset
    28
#import <string.h>
821faf99fb06 8074836: Resolve disabled warnings for libosxkrb5
weijun
parents: 25859
diff changeset
    29
#import <time.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29491
diff changeset
    31
#include "jni_util.h"
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29491
diff changeset
    32
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
 * Based largely on klist.c,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
 * Created by Scott Kovatch on 8/12/04.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
 * See http://www.opensource.apple.com/darwinsource/10.3.3/Kerberos-47/KerberosClients/klist/Sources/klist.c
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    41
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    42
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    43
 * Statics for this module
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
static jclass derValueClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
static jclass ticketClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    48
static jclass principalNameClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
static jclass encryptionKeyClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
static jclass ticketFlagsClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
static jclass kerberosTimeClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
static jclass javaLangStringClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
static jclass javaLangIntegerClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
static jclass hostAddressClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
static jclass hostAddressesClass = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
static jmethodID derValueConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
static jmethodID ticketConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
static jmethodID principalNameConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
static jmethodID encryptionKeyConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
static jmethodID ticketFlagsConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
static jmethodID kerberosTimeConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
static jmethodID krbcredsConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
static jmethodID integerConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
static jmethodID hostAddressConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
static jmethodID hostAddressesConstructor = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    69
 * Function prototypes for internal routines
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
static jobject BuildTicket(JNIEnv *env, krb5_data *encodedTicket);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
static jobject BuildClientPrincipal(JNIEnv *env, krb5_context kcontext, krb5_principal principalName);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
static jobject BuildEncryptionKey(JNIEnv *env, krb5_keyblock *cryptoKey);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
static jobject BuildTicketFlags(JNIEnv *env, krb5_flags flags);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
static jobject BuildKerberosTime(JNIEnv *env, krb5_timestamp kerbtime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
static jobject BuildAddressList(JNIEnv *env, krb5_address **kerbtime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
static void printiferr (errcode_t err, const char *format, ...);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
static jclass FindClass(JNIEnv *env, char *className)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    82
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
    jclass cls = (*env)->FindClass(env, className);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    84
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
    if (cls == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    86
        printf("Couldn't find %s\n", className);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    87
        return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    89
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
    jobject returnValue = (*env)->NewWeakGlobalRef(env,cls);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
    return returnValue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
 * Class:     sun_security_krb5_KrbCreds
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
 * Method:    JNI_OnLoad
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
 */
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29491
diff changeset
    97
JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *jvm, void *reserved)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
    JNIEnv *env;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
        return JNI_EVERSION; /* JNI version not supported */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
    ticketClass = FindClass(env, "sun/security/krb5/internal/Ticket");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
    if (ticketClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   107
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
    principalNameClass = FindClass(env, "sun/security/krb5/PrincipalName");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
    if (principalNameClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
    derValueClass = FindClass(env, "sun/security/util/DerValue");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   112
    if (derValueClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   114
    encryptionKeyClass = FindClass(env, "sun/security/krb5/EncryptionKey");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
    if (encryptionKeyClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
    ticketFlagsClass = FindClass(env,"sun/security/krb5/internal/TicketFlags");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   118
    if (ticketFlagsClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
    kerberosTimeClass = FindClass(env,"sun/security/krb5/internal/KerberosTime");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
    if (kerberosTimeClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
    javaLangStringClass = FindClass(env,"java/lang/String");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
    if (javaLangStringClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   125
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
    javaLangIntegerClass = FindClass(env,"java/lang/Integer");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
    if (javaLangIntegerClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   129
    hostAddressClass = FindClass(env,"sun/security/krb5/internal/HostAddress");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
    if (hostAddressClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
    hostAddressesClass = FindClass(env,"sun/security/krb5/internal/HostAddresses");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
    if (hostAddressesClass == NULL) return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   134
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   135
    derValueConstructor = (*env)->GetMethodID(env, derValueClass, "<init>", "([B)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
    if (derValueConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   137
        printf("Couldn't find DerValue constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   139
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   140
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   141
    ticketConstructor = (*env)->GetMethodID(env, ticketClass, "<init>", "(Lsun/security/util/DerValue;)V");
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   142
    if (ticketConstructor == 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   143
        printf("Couldn't find Ticket constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   145
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   146
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   147
    principalNameConstructor = (*env)->GetMethodID(env, principalNameClass, "<init>", "(Ljava/lang/String;I)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
    if (principalNameConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
        printf("Couldn't find PrincipalName constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   152
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
    encryptionKeyConstructor = (*env)->GetMethodID(env, encryptionKeyClass, "<init>", "(I[B)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
    if (encryptionKeyConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
        printf("Couldn't find EncryptionKey constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   156
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   157
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
    ticketFlagsConstructor = (*env)->GetMethodID(env, ticketFlagsClass, "<init>", "(I[B)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
    if (ticketFlagsConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
        printf("Couldn't find TicketFlags constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   163
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   165
    kerberosTimeConstructor = (*env)->GetMethodID(env, kerberosTimeClass, "<init>", "(J)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   166
    if (kerberosTimeConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   167
        printf("Couldn't find KerberosTime constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   168
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   169
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   170
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
    integerConstructor = (*env)->GetMethodID(env, javaLangIntegerClass, "<init>", "(I)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   172
    if (integerConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
        printf("Couldn't find Integer constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   174
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   175
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   176
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   177
    hostAddressConstructor = (*env)->GetMethodID(env, hostAddressClass, "<init>", "(I[B)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   178
    if (hostAddressConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   179
        printf("Couldn't find HostAddress constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   180
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   181
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   182
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   183
    hostAddressesConstructor = (*env)->GetMethodID(env, hostAddressesClass, "<init>", "([Lsun/security/krb5/internal/HostAddress;)V");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   184
    if (hostAddressesConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   185
        printf("Couldn't find HostAddresses constructor\n");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   186
        return JNI_ERR;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   187
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   188
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   189
    return JNI_VERSION_1_2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   191
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   192
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   193
 * Class:     sun_security_jgss_KrbCreds
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   194
 * Method:    JNI_OnUnload
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   195
 */
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29491
diff changeset
   196
JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *jvm, void *reserved)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   197
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
    JNIEnv *env;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   199
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   200
    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
        return; /* Nothing else we can do */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   202
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   203
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   204
    if (ticketClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   205
        (*env)->DeleteWeakGlobalRef(env,ticketClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   206
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   207
    if (derValueClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   208
        (*env)->DeleteWeakGlobalRef(env,derValueClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   209
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   210
    if (principalNameClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   211
        (*env)->DeleteWeakGlobalRef(env,principalNameClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   212
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   213
    if (encryptionKeyClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   214
        (*env)->DeleteWeakGlobalRef(env,encryptionKeyClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   215
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   216
    if (ticketFlagsClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   217
        (*env)->DeleteWeakGlobalRef(env,ticketFlagsClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   218
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
    if (kerberosTimeClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
        (*env)->DeleteWeakGlobalRef(env,kerberosTimeClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   222
    if (javaLangStringClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
        (*env)->DeleteWeakGlobalRef(env,javaLangStringClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
    if (javaLangIntegerClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
        (*env)->DeleteWeakGlobalRef(env,javaLangIntegerClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   227
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   228
    if (hostAddressClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
        (*env)->DeleteWeakGlobalRef(env,hostAddressClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   230
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   231
    if (hostAddressesClass != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   232
        (*env)->DeleteWeakGlobalRef(env,hostAddressesClass);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   233
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   234
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   235
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   236
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   237
int isIn(krb5_enctype e, int n, jint* etypes)
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   238
{
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   239
    int i;
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   240
    for (i=0; i<n; i++) {
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   241
        if (e == etypes[i]) return 1;
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   242
    }
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   243
    return 0;
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   244
}
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   245
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
 * Class:     sun_security_krb5_Credentials
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
 * Method:    acquireDefaultNativeCreds
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   249
 * Signature: ([I])Lsun/security/krb5/Credentials;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
JNIEXPORT jobject JNICALL Java_sun_security_krb5_Credentials_acquireDefaultNativeCreds
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   252
(JNIEnv *env, jclass krbcredsClass, jintArray jetypes)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
    jobject krbCreds = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   255
    krb5_error_code err = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
    krb5_ccache ccache = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
    krb5_cc_cursor cursor = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
    krb5_creds creds;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   259
    krb5_flags flags = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
    krb5_context kcontext = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   262
    int netypes;
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   263
    jint *etypes = NULL;
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   264
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   265
    /* Initialize the Kerberos 5 context */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   266
    err = krb5_init_context (&kcontext);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   267
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   268
    if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   269
        err = krb5_cc_default (kcontext, &ccache);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   270
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   271
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   272
    if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
        err = krb5_cc_set_flags (kcontext, ccache, flags); /* turn off OPENCLOSE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   274
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   275
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   276
    if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   277
        err = krb5_cc_start_seq_get (kcontext, ccache, &cursor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   278
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   280
    netypes = (*env)->GetArrayLength(env, jetypes);
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   281
    etypes = (jint *) (*env)->GetIntArrayElements(env, jetypes, NULL);
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   282
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   283
    if (etypes != NULL && !err) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   284
        while ((err = krb5_cc_next_cred (kcontext, ccache, &cursor, &creds)) == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   285
            char *serverName = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   286
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   287
            if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   288
                err = krb5_unparse_name (kcontext, creds.server, &serverName);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   289
                printiferr (err, "while unparsing server name");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   290
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   291
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
            if (!err) {
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   293
                char* slash = strchr(serverName, '/');
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   294
                char* at = strchr(serverName, '@');
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   295
                // Make sure the server's name is krbtgt/REALM@REALM, the etype
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   296
                // is supported, and the ticket has not expired
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   297
                if (slash && at &&
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   298
                        strncmp (serverName, "krbtgt", slash-serverName) == 0 &&
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   299
                            // the ablove line shows at must be after slash
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   300
                        strncmp (slash+1, at+1, at-slash-1) == 0 &&
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   301
                        isIn (creds.keyblock.enctype, netypes, etypes) &&
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   302
                        creds.times.endtime > time(0)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
                    jobject ticket, clientPrincipal, targetPrincipal, encryptionKey;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
                    jobject ticketFlags, startTime, endTime;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
                    jobject authTime, renewTillTime, hostAddresses;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
                    ticket = clientPrincipal = targetPrincipal = encryptionKey = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
                    ticketFlags = startTime = endTime = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
                    authTime = renewTillTime = hostAddresses = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   311
                    // For the default credentials we're only interested in the krbtgt server.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
                    clientPrincipal = BuildClientPrincipal(env, kcontext, creds.client);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
                    if (clientPrincipal == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   314
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   315
                    targetPrincipal = BuildClientPrincipal(env, kcontext, creds.server);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
                    if (targetPrincipal == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   317
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   318
                    // Build a sun/security/krb5/internal/Ticket
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
                    ticket = BuildTicket(env, &creds.ticket);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   320
                    if (ticket == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   322
                    // Get the encryption key
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   323
                    encryptionKey = BuildEncryptionKey(env, &creds.keyblock);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   324
                    if (encryptionKey == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
                    // and the ticket flags
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
                    ticketFlags = BuildTicketFlags(env, creds.ticket_flags);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   328
                    if (ticketFlags == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
                    // Get the timestamps out.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
                    startTime = BuildKerberosTime(env, creds.times.starttime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
                    if (startTime == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
                    authTime = BuildKerberosTime(env, creds.times.authtime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
                    if (authTime == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   336
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   337
                    endTime = BuildKerberosTime(env, creds.times.endtime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   338
                    if (endTime == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   339
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   340
                    renewTillTime = BuildKerberosTime(env, creds.times.renew_till);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   341
                    if (renewTillTime == NULL) goto cleanup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   342
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   343
                    // Create the addresses object.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
                    hostAddresses = BuildAddressList(env, creds.addresses);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   345
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   346
                    if (krbcredsConstructor == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   347
                        krbcredsConstructor = (*env)->GetMethodID(env, krbcredsClass, "<init>",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   348
                                                                  "(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");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   349
                        if (krbcredsConstructor == 0) {
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   350
                            printf("Couldn't find sun.security.krb5.internal.Ticket constructor\n");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   351
                            break;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   352
                        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   355
                    // and now go build a KrbCreds object
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   356
                    krbCreds = (*env)->NewObject(
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   357
                                                 env,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   358
                                                 krbcredsClass,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   359
                                                 krbcredsConstructor,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   360
                                                 ticket,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   361
                                                 clientPrincipal,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   362
                                                 targetPrincipal,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   363
                                                 encryptionKey,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   364
                                                 ticketFlags,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   365
                                                 authTime,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   366
                                                 startTime,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   367
                                                 endTime,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   368
                                                 renewTillTime,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   369
                                                 hostAddresses);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   370
cleanup:
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   371
                    if (ticket) (*env)->DeleteLocalRef(env, ticket);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   372
                    if (clientPrincipal) (*env)->DeleteLocalRef(env, clientPrincipal);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   373
                    if (targetPrincipal) (*env)->DeleteLocalRef(env, targetPrincipal);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   374
                    if (encryptionKey) (*env)->DeleteLocalRef(env, encryptionKey);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   375
                    if (ticketFlags) (*env)->DeleteLocalRef(env, ticketFlags);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   376
                    if (authTime) (*env)->DeleteLocalRef(env, authTime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   377
                    if (startTime) (*env)->DeleteLocalRef(env, startTime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   378
                    if (endTime) (*env)->DeleteLocalRef(env, endTime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   379
                    if (renewTillTime) (*env)->DeleteLocalRef(env, renewTillTime);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   380
                    if (hostAddresses) (*env)->DeleteLocalRef(env, hostAddresses);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   381
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   382
                    // Stop if there is an exception or we already found the initial TGT
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   383
                    if ((*env)->ExceptionCheck(env) || krbCreds) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   384
                        break;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   385
                    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   386
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   387
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   388
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   389
            if (serverName != NULL) { krb5_free_unparsed_name (kcontext, serverName); }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   390
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   391
            krb5_free_cred_contents (kcontext, &creds);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   392
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   393
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   394
        if (err == KRB5_CC_END) { err = 0; }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   395
        printiferr (err, "while retrieving a ticket");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   396
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   397
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   398
    if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   399
        err = krb5_cc_end_seq_get (kcontext, ccache, &cursor);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   400
        printiferr (err, "while finishing ticket retrieval");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   401
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   402
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   403
    if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   404
        flags = KRB5_TC_OPENCLOSE; /* restore OPENCLOSE mode */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   405
        err = krb5_cc_set_flags (kcontext, ccache, flags);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   406
        printiferr (err, "while finishing ticket retrieval");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   407
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   408
19373
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   409
    if (etypes != NULL) {
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   410
        (*env)->ReleaseIntArrayElements(env, jetypes, etypes, 0);
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   411
    }
4bb12c72a46f 8016594: Native Windows ccache still reads DES tickets
weijun
parents: 14342
diff changeset
   412
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   413
    krb5_free_context (kcontext);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   414
    return krbCreds;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   415
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   416
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   417
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   418
#pragma mark -
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   419
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   420
jobject BuildTicket(JNIEnv *env, krb5_data *encodedTicket)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   421
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   422
    /* To build a Ticket, we first need to build a DerValue out of the EncodedTicket.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   423
    * But before we can do that, we need to make a byte array out of the ET.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   424
    */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   425
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   426
    jobject derValue, ticket;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   427
    jbyteArray ary;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   428
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   429
    ary = (*env)->NewByteArray(env, encodedTicket->length);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   430
    if ((*env)->ExceptionCheck(env)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   431
        return (jobject) NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   432
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   433
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   434
    (*env)->SetByteArrayRegion(env, ary, (jsize) 0, encodedTicket->length, (jbyte *)encodedTicket->data);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   435
    if ((*env)->ExceptionCheck(env)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   436
        (*env)->DeleteLocalRef(env, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   437
        return (jobject) NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   438
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   439
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   440
    derValue = (*env)->NewObject(env, derValueClass, derValueConstructor, ary);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   441
    if ((*env)->ExceptionCheck(env)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   442
        (*env)->DeleteLocalRef(env, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   443
        return (jobject) NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   444
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   445
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   446
    (*env)->DeleteLocalRef(env, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   447
    ticket = (*env)->NewObject(env, ticketClass, ticketConstructor, derValue);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   448
    if ((*env)->ExceptionCheck(env)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   449
        (*env)->DeleteLocalRef(env, derValue);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   450
        return (jobject) NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   451
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   452
    (*env)->DeleteLocalRef(env, derValue);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   453
    return ticket;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   454
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   455
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   456
jobject BuildClientPrincipal(JNIEnv *env, krb5_context kcontext, krb5_principal principalName) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   457
    // Get the full principal string.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   458
    char *principalString = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   459
    jobject principal = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   460
    int err = krb5_unparse_name (kcontext, principalName, &principalString);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   461
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   462
    if (!err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   463
        // Make a PrincipalName from the full string and the type.  Let the PrincipalName class parse it out.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   464
        jstring principalStringObj = (*env)->NewStringUTF(env, principalString);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   465
        if (principalStringObj == NULL) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   466
            if (principalString != NULL) { krb5_free_unparsed_name (kcontext, principalString); }
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   467
            return (jobject) NULL;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   468
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   469
        principal = (*env)->NewObject(env, principalNameClass, principalNameConstructor, principalStringObj, principalName->type);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   470
        if (principalString != NULL) { krb5_free_unparsed_name (kcontext, principalString); }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   471
        (*env)->DeleteLocalRef(env, principalStringObj);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   472
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   473
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   474
    return principal;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   475
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   476
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   477
jobject BuildEncryptionKey(JNIEnv *env, krb5_keyblock *cryptoKey) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   478
    // First, need to build a byte array
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   479
    jbyteArray ary;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   480
    jobject encryptionKey = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   481
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   482
    ary = (*env)->NewByteArray(env,cryptoKey->length);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   483
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   484
    if (ary == NULL) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   485
        return (jobject) NULL;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   486
    }
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   487
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   488
    (*env)->SetByteArrayRegion(env, ary, (jsize) 0, cryptoKey->length, (jbyte *)cryptoKey->contents);
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   489
    if (!(*env)->ExceptionCheck(env)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   490
        encryptionKey = (*env)->NewObject(env, encryptionKeyClass, encryptionKeyConstructor, cryptoKey->enctype, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   491
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   492
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   493
    (*env)->DeleteLocalRef(env, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   494
    return encryptionKey;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   495
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   496
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   497
jobject BuildTicketFlags(JNIEnv *env, krb5_flags flags) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   498
    jobject ticketFlags = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   499
    jbyteArray ary;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   500
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   501
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   502
     * Convert the bytes to network byte order before copying
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   503
     * them to a Java byte array.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   504
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   505
    unsigned long nlflags = htonl(flags);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   506
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   507
    ary = (*env)->NewByteArray(env, sizeof(flags));
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   508
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   509
    if (ary == NULL) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   510
        return (jobject) NULL;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   511
    }
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   512
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   513
    (*env)->SetByteArrayRegion(env, ary, (jsize) 0, sizeof(flags), (jbyte *)&nlflags);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   514
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   515
    if (!(*env)->ExceptionCheck(env)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   516
        ticketFlags = (*env)->NewObject(env, ticketFlagsClass, ticketFlagsConstructor, sizeof(flags)*8, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   517
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   518
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   519
    (*env)->DeleteLocalRef(env, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   520
    return ticketFlags;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   521
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   522
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   523
jobject BuildKerberosTime(JNIEnv *env, krb5_timestamp kerbtime) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   524
    jlong time = kerbtime;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   525
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   526
    // Kerberos time is in seconds, but the KerberosTime class assumes milliseconds, so multiply by 1000.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   527
    time *= 1000;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   528
    return (*env)->NewObject(env, kerberosTimeClass, kerberosTimeConstructor, time);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   529
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   530
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   531
jobject BuildAddressList(JNIEnv *env, krb5_address **addresses) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   532
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   533
    if (addresses == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   534
        return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   535
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   536
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   537
    int addressCount = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   538
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   539
    // See how many we have.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   540
    krb5_address **p = addresses;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   541
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   542
    while (*p != 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   543
        addressCount++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   544
        p++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   545
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   546
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   547
    jobject address_list = (*env)->NewObjectArray(env, addressCount, hostAddressClass, NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   548
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   549
    if (address_list == NULL) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   550
        return (jobject) NULL;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   551
    }
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   552
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   553
    // Create a new HostAddress object for each address block.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   554
    // First, reset the iterator.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   555
    p = addresses;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   556
    jsize index = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   557
    while (*p != 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   558
        krb5_address *currAddress = *p;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   559
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   560
        // HostAddres needs a byte array of the host data.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   561
        jbyteArray ary = (*env)->NewByteArray(env, currAddress->length);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   562
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   563
        if (ary == NULL) return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   564
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   565
        (*env)->SetByteArrayRegion(env, ary, (jsize) 0, currAddress->length, (jbyte *)currAddress->contents);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   566
        jobject address = (*env)->NewObject(env, hostAddressClass, hostAddressConstructor, currAddress->length, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   567
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   568
        (*env)->DeleteLocalRef(env, ary);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   569
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   570
        if (address == NULL) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   571
            return (jobject) NULL;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   572
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   573
        // Add the HostAddress to the arrray.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   574
        (*env)->SetObjectArrayElement(env, address_list, index, address);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   575
22986
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   576
        if ((*env)->ExceptionCheck(env)) {
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   577
            return (jobject) NULL;
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   578
        }
d2db7c5718ca 8034033: [parfait] JNI exception pending in share/native/sun/security/krb5/nativeccache.c
weijun
parents: 19373
diff changeset
   579
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   580
        index++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   581
        p++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   582
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   583
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   584
    return address_list;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   585
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   586
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   587
#pragma mark - Utility methods -
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   588
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   589
static void printiferr (errcode_t err, const char *format, ...)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   590
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   591
    if (err) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   592
        va_list pvar;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   593
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   594
        va_start (pvar, format);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   595
        com_err_va ("ticketParser:", err, format, pvar);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   596
        va_end (pvar);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   597
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   598
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   599