jdk/src/share/classes/sun/security/jgss/krb5/Krb5Util.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2942 37d9baeb7518
child 9499 f3115698a012
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
     2
 * Copyright (c) 2003, 2009, 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: 2942
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: 2942
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: 2942
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
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
package sun.security.jgss.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.security.auth.kerberos.KerberosTicket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.kerberos.KerberosKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.kerberos.KerberosPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jgss.GSSUtil;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    35
import sun.security.jgss.GSSCaller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.Credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.EncryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Utilities for obtaining and converting Kerberos tickets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Krb5Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final boolean DEBUG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            new sun.security.action.GetBooleanAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            ("sun.security.krb5.debug")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Default constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Krb5Util() {  // Cannot create one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Retrieve the service ticket for serverPrincipal from caller's Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * or from Subject obtained by logging in, or if not found, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Ticket Granting Service using the TGT obtained from the Subject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Caller must have permission to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *    - access and update Subject's private credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *    - create LoginContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *    - read the auth.login.defaultCallbackHandler security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * NOTE: This method is used by JSSE Kerberos Cipher Suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    71
    public static KerberosTicket getTicketFromSubjectAndTgs(GSSCaller caller,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String clientPrincipal, String serverPrincipal, String tgsPrincipal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws LoginException, KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // 1. Try to find service ticket in acc subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Subject accSubj = Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        KerberosTicket ticket = (KerberosTicket) SubjectComber.find(accSubj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            serverPrincipal, clientPrincipal, KerberosTicket.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (ticket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return ticket;  // found it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Subject loginSubj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (!GSSUtil.useSubjectCredsOnly(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // 2. Try to get ticket from login
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                loginSubj = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                ticket = (KerberosTicket) SubjectComber.find(loginSubj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    serverPrincipal, clientPrincipal, KerberosTicket.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (ticket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    return ticket; // found it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } catch (LoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // No login entry to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                // ignore and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Service ticket not found in subject or login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Try to get TGT to acquire service ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // 3. Try to get TGT from acc subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        KerberosTicket tgt = (KerberosTicket) SubjectComber.find(accSubj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            tgsPrincipal, clientPrincipal, KerberosTicket.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        boolean fromAcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (tgt == null && loginSubj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // 4. Try to get TGT from login subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            tgt = (KerberosTicket) SubjectComber.find(loginSubj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                tgsPrincipal, clientPrincipal, KerberosTicket.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            fromAcc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            fromAcc = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // 5. Try to get service ticket using TGT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (tgt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            Credentials tgtCreds = ticketToCreds(tgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Credentials serviceCreds = Credentials.acquireServiceCreds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        serverPrincipal, tgtCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (serviceCreds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ticket = credsToTicket(serviceCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // Store service ticket in acc's Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (fromAcc && accSubj != null && !accSubj.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    accSubj.getPrivateCredentials().add(ticket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Retrieves the ticket corresponding to the client/server principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * pair from the Subject in the specified AccessControlContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * If the ticket can not be found in the Subject, and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * useSubjectCredsOnly is false, then obtain ticket from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * a LoginContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   142
    static KerberosTicket getTicket(GSSCaller caller,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String clientPrincipal, String serverPrincipal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        AccessControlContext acc) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Try to get ticket from acc's Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Subject accSubj = Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        KerberosTicket ticket = (KerberosTicket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            SubjectComber.find(accSubj, serverPrincipal, clientPrincipal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                  KerberosTicket.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // Try to get ticket from Subject obtained from GSSUtil
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (ticket == null && !GSSUtil.useSubjectCredsOnly(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ticket = (KerberosTicket) SubjectComber.find(subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                serverPrincipal, clientPrincipal, KerberosTicket.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return ticket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Retrieves the caller's Subject, or Subject obtained by logging in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * via the specified caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Caller must have permission to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *    - access the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *    - create LoginContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *    - read the auth.login.defaultCallbackHandler security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * NOTE: This method is used by JSSE Kerberos Cipher Suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   172
    public static Subject getSubject(GSSCaller caller,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        AccessControlContext acc) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // Try to get the Subject from acc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Subject subject = Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Try to get Subject obtained from GSSUtil
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (subject == null && !GSSUtil.useSubjectCredsOnly(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Retrieves the keys for the specified server principal from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the Subject in the specified AccessControlContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * If the ticket can not be found in the Subject, and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * useSubjectCredsOnly is false, then obtain keys from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * a LoginContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * NOTE: This method is used by JSSE Kerberos Cipher Suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   194
    public static KerberosKey[] getKeys(GSSCaller caller,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        String serverPrincipal, AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        Subject accSubj = Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        List<KerberosKey> kkeys = (List<KerberosKey>)SubjectComber.findMany(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                accSubj, serverPrincipal, null, KerberosKey.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (kkeys == null && !GSSUtil.useSubjectCredsOnly(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            kkeys = (List<KerberosKey>) SubjectComber.findMany(subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                serverPrincipal, null, KerberosKey.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (kkeys != null && (len = kkeys.size()) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            KerberosKey[] keys = new KerberosKey[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            kkeys.toArray(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
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 static KerberosTicket credsToTicket(Credentials serviceCreds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        EncryptionKey sessionKey =  serviceCreds.getSessionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return new KerberosTicket(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            serviceCreds.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            new KerberosPrincipal(serviceCreds.getClient().getName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            new KerberosPrincipal(serviceCreds.getServer().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                KerberosPrincipal.KRB_NT_SRV_INST),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            sessionKey.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            sessionKey.getEType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            serviceCreds.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            serviceCreds.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            serviceCreds.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            serviceCreds.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            serviceCreds.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            serviceCreds.getClientAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static Credentials ticketToCreds(KerberosTicket kerbTicket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return new Credentials(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            kerbTicket.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            kerbTicket.getClient().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            kerbTicket.getServer().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            kerbTicket.getSessionKey().getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            kerbTicket.getSessionKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            kerbTicket.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            kerbTicket.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            kerbTicket.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            kerbTicket.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            kerbTicket.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            kerbTicket.getClientAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}