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