jdk/src/share/classes/sun/security/krb5/Credentials.java
author weijun
Tue, 11 Aug 2009 12:20:32 +0800
changeset 3483 a16fce1820ef
parent 2 90ce3da70b43
child 3949 00603a93b589
permissions -rw-r--r--
6821190: more InquireType values for ExtendedGSSContext Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
     2
 * Portions Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
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. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.ccache.CredentialsCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.internal.ktab.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.internal.crypto.EType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This class encapsulates the concept of a Kerberos service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * credential. That includes a Kerberos ticket and an associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class Credentials {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    Ticket ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    PrincipalName client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    PrincipalName server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    EncryptionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    TicketFlags flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    KerberosTime authTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    KerberosTime startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    KerberosTime endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    KerberosTime renewTill;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    HostAddresses cAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    EncryptionKey serviceKey;
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    66
    AuthorizationData authzData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static CredentialsCache cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static boolean alreadyLoaded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static boolean alreadyTried = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static native Credentials acquireDefaultNativeCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public Credentials(Ticket new_ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                       PrincipalName new_client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                       PrincipalName new_server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                       EncryptionKey new_key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                       TicketFlags new_flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                       KerberosTime authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                       KerberosTime new_startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                       KerberosTime new_endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                       KerberosTime renewTill,
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    82
                       HostAddresses cAddr,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    83
                       AuthorizationData authzData) {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    84
        this(new_ticket, new_client, new_server, new_key, new_flags,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    85
                authTime, new_startTime, new_endTime, renewTill, cAddr);
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    86
        this.authzData = authzData;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    87
    }
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    88
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    89
    public Credentials(Ticket new_ticket,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    90
                       PrincipalName new_client,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    91
                       PrincipalName new_server,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    92
                       EncryptionKey new_key,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    93
                       TicketFlags new_flags,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    94
                       KerberosTime authTime,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    95
                       KerberosTime new_startTime,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    96
                       KerberosTime new_endTime,
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
    97
                       KerberosTime renewTill,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                       HostAddresses cAddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ticket = new_ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        client = new_client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        server = new_server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        key = new_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        flags = new_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.authTime = authTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        startTime = new_startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        endTime = new_endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.renewTill = renewTill;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.cAddr = cAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public Credentials(byte[] encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                       String client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                       String server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                       byte[] keyBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                       int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                       boolean[] flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                       Date authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                       Date startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                       Date endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                       Date renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                       InetAddress[] cAddrs) throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this(new Ticket(encoding),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             new PrincipalName(client, PrincipalName.KRB_NT_PRINCIPAL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             new PrincipalName(server, PrincipalName.KRB_NT_SRV_INST),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             new EncryptionKey(keyType, keyBytes),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             (flags == null? null: new TicketFlags(flags)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             (authTime == null? null: new KerberosTime(authTime)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             (startTime == null? null: new KerberosTime(startTime)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             (endTime == null? null: new KerberosTime(endTime)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             (renewTill == null? null: new KerberosTime(renewTill)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             null); // caddrs are in the encoding at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Acquires a service ticket for the specified service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * principal. If the service ticket is not already available, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * obtains a new one from the KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Credentials(Credentials tgt, PrincipalName service)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public final PrincipalName getClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public final PrincipalName getServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public final EncryptionKey getSessionKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public final Date getAuthTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (authTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return authTime.toDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public final Date getStartTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (startTime != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return startTime.toDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public final Date getEndTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (endTime != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return endTime.toDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public final Date getRenewTill() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (renewTill != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return renewTill.toDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public final boolean[] getFlags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (flags == null) // Can be in a KRB-CRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return flags.toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public final InetAddress[] getClientAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (cAddr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return cAddr.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public final byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        byte[] retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            retVal = ticket.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } catch (Asn1Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            System.out.println(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public boolean isForwardable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return flags.get(Krb5.TKT_OPTS_FORWARDABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean isRenewable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return flags.get(Krb5.TKT_OPTS_RENEWABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public Ticket getTicket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public TicketFlags getTicketFlags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
   233
    public AuthorizationData getAuthzData() {
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
   234
        return authzData;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 2
diff changeset
   235
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Checks if the service ticket returned by the KDC has the OK-AS-DELEGATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * flag set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return true if OK-AS_DELEGATE flag is set, otherwise, return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public boolean checkDelegate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return (flags.get(Krb5.TKT_OPTS_DELEGATE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public Credentials renew() throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        KDCOptions options = new KDCOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        options.set(KDCOptions.RENEW, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * Added here to pass KrbKdcRep.check:73
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        options.set(KDCOptions.RENEWABLE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return new KrbTgsReq(options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                             this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                             server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                             null, // from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                             null, // till
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                             null, // rtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                             null, // eTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                             cAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                             null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                             null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                             null).sendAndGetCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Returns a TGT for the given client principal from a ticket cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param princ the client principal. A value of null means that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * default principal name in the credentials cache will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param ticketCache the path to the tickets file. A value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * of null will be accepted to indicate that the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * path should be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @returns the TGT credentials or null if none were found. If the tgt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * expired, it is the responsibility of the caller to determine this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public static Credentials acquireTGTFromCache(PrincipalName princ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                  String ticketCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (ticketCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // The default ticket cache on Windows is not a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            String os = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        new sun.security.action.GetPropertyAction("os.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (os.toUpperCase().startsWith("WINDOWS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                Credentials creds = acquireDefaultCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (creds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        System.out.println(">>> Found no TGT's in LSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (princ != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    if (creds.getClient().equals(princ)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            System.out.println(">>> Obtained TGT from LSA: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                               + creds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        return creds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            System.out.println(">>> LSA contains TGT for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                               + creds.getClient()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                               + " not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                               + princ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        System.out.println(">>> Obtained TGT from LSA: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                           + creds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    return creds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * Returns the appropriate cache. If ticketCache is null, it is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * default cache otherwise it is the cache filename contained in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        CredentialsCache ccache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            CredentialsCache.getInstance(princ, ticketCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (ccache == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        sun.security.krb5.internal.ccache.Credentials tgtCred  =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            ccache.getDefaultCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (EType.isSupported(tgtCred.getEType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return tgtCred.setKrbCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    ">>> unsupported key type found the default TGT: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    tgtCred.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns a TGT for the given client principal via an AS-Exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * This method causes pre-authentication data to be sent in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * AS-REQ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param princ the client principal. This value cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param secretKey the secret key of the client principal.This value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @returns the TGT credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public static Credentials acquireTGT(PrincipalName princ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                         EncryptionKey[] secretKeys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                         char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (princ == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        "Cannot have null principal to do AS-Exchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (secretKeys == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        "Cannot have null secretKey to do AS-Exchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        KrbAsRep asRep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            asRep = sendASRequest(princ, secretKeys, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (KrbException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if ((ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                // process pre-auth info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    System.out.println("AcquireTGT: PREAUTH FAILED/REQUIRED," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                " re-send AS-REQ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                KRBError error = ke.getError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                // update salt in PrincipalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                byte[] newSalt = error.getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (newSalt != null && newSalt.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    princ.setSalt(new String(newSalt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                // refresh keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    secretKeys = EncryptionKey.acquireSecretKeys(password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                princ.getSalt(), true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                error.getEType(), error.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                asRep = sendASRequest(princ, secretKeys, ke.getError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return asRep.getCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Sends the AS-REQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private static KrbAsRep sendASRequest(PrincipalName princ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        EncryptionKey[] secretKeys, KRBError error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // %%%
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        KrbAsReq asReq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (error == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            asReq = new KrbAsReq(princ, secretKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            asReq = new KrbAsReq(princ, secretKeys, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        error.getEType(), error.getSalt(), error.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        String kdc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        KrbAsRep asRep  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            kdc = asReq.send();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            asRep =  asReq.getReply(secretKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } catch (KrbException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (ke.returnCode() == Krb5.KRB_ERR_RESPONSE_TOO_BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    asReq.send(princ.getRealmString(), kdc, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    asRep =  asReq.getReply(secretKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return asRep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Acquires default credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <br>The possible locations for default credentials cache is searched in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <li> The directory and cache file name specified by "KRB5CCNAME" system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <li> The directory and cache file name specified by "KRB5CCNAME"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * environment variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <li> A cache file named krb5cc_{user.name} at {user.home} directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return a <code>KrbCreds</code> object if the credential is found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * otherwise return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    // this method is intentionally changed to not check if the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    // principal name matches cache file's principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    // It assumes that the GSS call has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    // the privilege to access the default cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public static synchronized Credentials acquireDefaultCreds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Credentials result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            cache = CredentialsCache.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                System.out.println(">>> KrbCreds found the default ticket " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                   "granting ticket in credential cache.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            sun.security.krb5.internal.ccache.Credentials temp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                cache.getDefaultCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (EType.isSupported(temp.getEType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                result = temp.setKrbCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        ">>> unsupported key type found the default TGT: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        temp.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // Doesn't seem to be a default cache on this system or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // TGT has unsupported encryption type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (!alreadyTried) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                // See if there's any native code to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    ensureLoaded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        System.out.println("Can not load credentials cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    alreadyTried = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (alreadyLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                // There is some native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                   System.out.println(">> Acquire default native Credentials");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                result = acquireDefaultNativeCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                // only TGT with DES key will be returned by native method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Gets service credential from key table. The credential is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * decrypt the received client message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * and authenticate the client by verifying the client's credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param serviceName the name of service, using format component@realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param keyTabFile the file of key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @return a <code>KrbCreds</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public static Credentials getServiceCreds(String serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                              File keyTabFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        EncryptionKey k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        PrincipalName service = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        Credentials result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            service = new PrincipalName(serviceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (service.getRealm() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                String realm = Config.getInstance().getDefaultRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if (realm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    service.setRealm(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        } catch (RealmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        KeyTab kt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (keyTabFile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            kt = KeyTab.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            kt = KeyTab.getInstance(keyTabFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if ((kt != null) && (kt.findServiceEntry(service))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            k = kt.readServiceKey(service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            result = new Credentials(null, service, null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                     null, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            result.serviceKey = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Acquires credentials for a specified service using initial credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * When the service has a different realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * from the initial credential, we do cross-realm authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * - first, we use the current credential to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * a cross-realm credential from the local KDC, then use that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * cross-realm credential to request service credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * from the foreigh KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param service the name of service principal using format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * components@realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @param ccreds client's initial credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @exception IOException if an error occurs in reading the credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @exception KrbException if an error occurs specific to Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @return a <code>Credentials</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public static Credentials acquireServiceCreds(String service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                                  Credentials ccreds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return CredentialsUtil.acquireServiceCreds(service, ccreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * This method does the real job to request the service credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private static Credentials serviceCreds(ServiceName service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                            Credentials ccreds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return new KrbTgsReq(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                new KDCOptions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                ccreds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                null, // KerberosTime from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                null, // KerberosTime till
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                null, // KerberosTime rtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                null, // int[] eTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                null, // HostAddresses addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                null, // AuthorizationData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                null, // Ticket[] additionalTickets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                null  // EncryptionKey subSessionKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                ).sendAndGetCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public CredentialsCache getCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public EncryptionKey getServiceKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return serviceKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Prints out debug info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public static void printDebug(Credentials c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        System.out.println(">>> DEBUG: ----Credentials----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        System.out.println("\tclient: " + c.client.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        System.out.println("\tserver: " + c.server.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        System.out.println("\tticket: realm: " + c.ticket.realm.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        System.out.println("\t        sname: " + c.ticket.sname.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (c.startTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            System.out.println("\tstartTime: " + c.startTime.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        System.out.println("\tendTime: " + c.endTime.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        System.out.println("        ----Credentials end----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    static void ensureLoaded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                new java.security.PrivilegedAction<Void> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                System.loadLibrary("w2k_lsa_auth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        alreadyLoaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        StringBuffer buffer = new StringBuffer("Credentials:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        buffer.append("\nclient=").append(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        buffer.append("\nserver=").append(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (authTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            buffer.append("\nauthTime=").append(authTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (startTime != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            buffer.append("\nstartTime=").append(startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        buffer.append("\nendTime=").append(endTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        buffer.append("\nrenewTill=").append(renewTill);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        buffer.append("\nflags: ").append(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        buffer.append("\nEType (int): ").append(key.getEType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
}