Krb5AcceptCredential.java 6.4 KB
Newer Older
D
duke 已提交
1
/*
W
weijun 已提交
2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27
 */

package sun.security.jgss.krb5;

28
import java.io.IOException;
D
duke 已提交
29
import org.ietf.jgss.*;
30
import sun.security.jgss.GSSCaller;
D
duke 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import sun.security.jgss.spi.*;
import sun.security.krb5.*;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.AccessController;
import java.security.AccessControlContext;
import javax.security.auth.DestroyFailedException;

/**
 * Implements the krb5 acceptor credential element.
 *
 * @author Mayank Upadhyay
 * @since 1.4
 */
public class Krb5AcceptCredential
    implements Krb5CredElement {

48 49
    private final Krb5NameElement name;
    private final ServiceCreds screds;
D
duke 已提交
50

51
    private Krb5AcceptCredential(Krb5NameElement name, ServiceCreds creds) {
D
duke 已提交
52 53 54 55 56 57 58
        /*
         * Initialize this instance with the data from the acquired
         * KerberosKey. This class needs to be a KerberosKey too
         * hence we can't just store a reference.
         */

        this.name = name;
W
weijun 已提交
59
        this.screds = creds;
D
duke 已提交
60 61
    }

62
    static Krb5AcceptCredential getInstance(final GSSCaller caller, Krb5NameElement name)
D
duke 已提交
63 64 65 66 67 68
        throws GSSException {

        final String serverPrinc = (name == null? null:
            name.getKrb5PrincipalName().getName());
        final AccessControlContext acc = AccessController.getContext();

69
        ServiceCreds creds = null;
D
duke 已提交
70
        try {
W
weijun 已提交
71
            creds = AccessController.doPrivileged(
72 73
                        new PrivilegedExceptionAction<ServiceCreds>() {
                public ServiceCreds run() throws Exception {
W
weijun 已提交
74
                    return Krb5Util.getServiceCreds(
75
                        caller == GSSCaller.CALLER_UNKNOWN ? GSSCaller.CALLER_ACCEPT: caller,
D
duke 已提交
76 77 78 79 80 81 82 83 84 85
                        serverPrinc, acc);
                }});
        } catch (PrivilegedActionException e) {
            GSSException ge =
                new GSSException(GSSException.NO_CRED, -1,
                    "Attempt to obtain new ACCEPT credentials failed!");
            ge.initCause(e.getException());
            throw ge;
        }

W
weijun 已提交
86
        if (creds == null)
D
duke 已提交
87
            throw new GSSException(GSSException.NO_CRED, -1,
W
weijun 已提交
88
                                   "Failed to find any Kerberos credentails");
D
duke 已提交
89 90

        if (name == null) {
W
weijun 已提交
91
            String fullName = creds.getName();
92 93
            if (fullName != null) {
                name = Krb5NameElement.getInstance(fullName,
D
duke 已提交
94
                                       Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
95
            }
D
duke 已提交
96 97
        }

W
weijun 已提交
98
        return new Krb5AcceptCredential(name, creds);
D
duke 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    }

    /**
     * Returns the principal name for this credential. The name
     * is in mechanism specific format.
     *
     * @return GSSNameSpi representing principal name of this credential
     * @exception GSSException may be thrown
     */
    public final GSSNameSpi getName() throws GSSException {
        return name;
    }

    /**
     * Returns the init lifetime remaining.
     *
     * @return the init lifetime remaining in seconds
     * @exception GSSException may be thrown
     */
    public int getInitLifetime() throws GSSException {
        return 0;
    }

    /**
     * Returns the accept lifetime remaining.
     *
     * @return the accept lifetime remaining in seconds
     * @exception GSSException may be thrown
     */
    public int getAcceptLifetime() throws GSSException {
        return GSSCredential.INDEFINITE_LIFETIME;
    }

    public boolean isInitiatorCredential() throws GSSException {
        return false;
    }

    public boolean isAcceptorCredential() throws GSSException {
        return true;
    }

    /**
     * Returns the oid representing the underlying credential
     * mechanism oid.
     *
     * @return the Oid for this credential mechanism
     * @exception GSSException may be thrown
     */
    public final Oid getMechanism() {
        return Krb5MechFactory.GSS_KRB5_MECH_OID;
    }

    public final java.security.Provider getProvider() {
        return Krb5MechFactory.PROVIDER;
    }

155 156
    public EncryptionKey[] getKrb5EncryptionKeys(PrincipalName princ) {
        return screds.getEKeys(princ);
D
duke 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    }

    /**
     * Called to invalidate this credential element.
     */
    public void dispose() throws GSSException {
        try {
            destroy();
        } catch (DestroyFailedException e) {
            GSSException gssException =
                new GSSException(GSSException.FAILURE, -1,
                 "Could not destroy credentials - " + e.getMessage());
            gssException.initCause(e);
        }
    }

    /**
     * Destroys the locally cached EncryptionKey value and then calls
     * destroy in the base class.
     */
    public void destroy() throws DestroyFailedException {
W
weijun 已提交
178
        screds.destroy();
D
duke 已提交
179
    }
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    /**
     * Impersonation is only available on the initiator side. The
     * service must starts as an initiator to get an initial TGT to complete
     * the S4U2self protocol.
     */
    @Override
    public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {
        Credentials cred = screds.getInitCred();
        if (cred != null) {
            return Krb5InitCredential.getInstance(this.name, cred)
                    .impersonate(name);
        } else {
            throw new GSSException(GSSException.FAILURE, -1,
                "Only an initiate credentials can impersonate");
        }
    }
D
duke 已提交
197
}