jdk/src/share/classes/sun/security/jgss/krb5/Krb5AcceptCredential.java
author weijun
Wed, 28 Sep 2011 14:21:10 +0800
changeset 10696 3811a12690ce
parent 9499 f3115698a012
child 14413 e954df027393
permissions -rw-r--r--
7077640: gss wrap for cfx doesn't handle rrc != 0 Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, 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 org.ietf.jgss.*;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    29
import sun.security.jgss.GSSCaller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.security.auth.DestroyFailedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Implements the krb5 acceptor credential element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Krb5AcceptCredential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    implements Krb5CredElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final long serialVersionUID = 7714332137352567952L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Krb5NameElement name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    51
    private Krb5Util.ServiceCreds screds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    53
    private Krb5AcceptCredential(Krb5NameElement name, Krb5Util.ServiceCreds creds) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * Initialize this instance with the data from the acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         * KerberosKey. This class needs to be a KerberosKey too
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         * hence we can't just store a reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.name = name;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    61
        this.screds = creds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    64
    static Krb5AcceptCredential getInstance(final GSSCaller caller, Krb5NameElement name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        final String serverPrinc = (name == null? null:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            name.getKrb5PrincipalName().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        final AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    71
        Krb5Util.ServiceCreds creds = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    73
            creds = AccessController.doPrivileged(
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    74
                        new PrivilegedExceptionAction<Krb5Util.ServiceCreds>() {
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    75
                public Krb5Util.ServiceCreds run() throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    76
                    return Krb5Util.getServiceCreds(
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    77
                        caller == GSSCaller.CALLER_UNKNOWN ? GSSCaller.CALLER_ACCEPT: caller,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        serverPrinc, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            GSSException ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    "Attempt to obtain new ACCEPT credentials failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ge.initCause(e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw ge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    88
        if (creds == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new GSSException(GSSException.NO_CRED, -1,
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    90
                                   "Failed to find any Kerberos credentails");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (name == null) {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    93
            String fullName = creds.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            name = Krb5NameElement.getInstance(fullName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                       Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    98
        return new Krb5AcceptCredential(name, creds);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns the principal name for this credential. The name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * is in mechanism specific format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return GSSNameSpi representing principal name of this credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public final GSSNameSpi getName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns the init lifetime remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the init lifetime remaining in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public int getInitLifetime() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Returns the accept lifetime remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return the accept lifetime remaining in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int getAcceptLifetime() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return GSSCredential.INDEFINITE_LIFETIME;
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 boolean isInitiatorCredential() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return false;
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 boolean isAcceptorCredential() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Returns the oid representing the underlying credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * mechanism oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the Oid for this credential mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public final Oid getMechanism() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return Krb5MechFactory.GSS_KRB5_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public final java.security.Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return Krb5MechFactory.PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    EncryptionKey[] getKrb5EncryptionKeys() {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
   156
        return screds.getEKeys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Called to invalidate this credential element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (DestroyFailedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 "Could not destroy credentials - " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Destroys the locally cached EncryptionKey value and then calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * destroy in the base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void destroy() throws DestroyFailedException {
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
   178
        screds.destroy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}