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