Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/krb5/Krb5ProxyCredential.java
41161 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.jgss.krb5;2627import org.ietf.jgss.*;28import sun.security.jgss.GSSCaller;29import sun.security.jgss.spi.*;3031import java.io.IOException;3233import sun.security.krb5.Credentials;34import sun.security.krb5.KrbException;35import sun.security.krb5.internal.Ticket;3637import javax.security.auth.kerberos.KerberosTicket;3839/**40* Implements the krb5 proxy credential element used in constrained41* delegation. It is used in both impersonation (where there is no Kerberos 542* communication between the middle server and the client) and normal43* constrained delegation (where there is, but client has not called44* requestCredDeleg(true)).45* @since 1.846*/4748public class Krb5ProxyCredential49implements Krb5CredElement {5051public final Krb5InitCredential self; // the middle server52private final Krb5NameElement client; // the client5354// The ticket with cname=client and sname=self. This can be a normal55// service ticket or an S4U2self ticket.56public final Ticket tkt;5758Krb5ProxyCredential(Krb5InitCredential self, Krb5NameElement client,59Ticket tkt) {60this.self = self;61this.tkt = tkt;62this.client = client;63}6465// The client name behind the proxy66@Override67public final Krb5NameElement getName() throws GSSException {68return client;69}7071@Override72public int getInitLifetime() throws GSSException {73// endTime of tkt is not used by KDC, and it's also not74// available in the case of kerberos constr deleg75return self.getInitLifetime();76}7778@Override79public int getAcceptLifetime() throws GSSException {80return 0;81}8283@Override84public boolean isInitiatorCredential() throws GSSException {85return true;86}8788@Override89public boolean isAcceptorCredential() throws GSSException {90return false;91}9293@Override94public final Oid getMechanism() {95return Krb5MechFactory.GSS_KRB5_MECH_OID;96}9798@Override99public final java.security.Provider getProvider() {100return Krb5MechFactory.PROVIDER;101}102103@Override104public void dispose() throws GSSException {105try {106self.destroy();107} catch (javax.security.auth.DestroyFailedException e) {108GSSException gssException =109new GSSException(GSSException.FAILURE, -1,110"Could not destroy credentials - " + e.getMessage());111gssException.initCause(e);112}113}114115@Override116public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {117// Cannot impersonate multiple levels without the impersonatee's TGT.118throw new GSSException(GSSException.FAILURE, -1,119"Only an initiate credentials can impersonate");120}121122// Try to see if a default credential should act as an impersonator.123static Krb5CredElement tryImpersonation(GSSCaller caller,124Krb5InitCredential initiator) throws GSSException {125126try {127KerberosTicket proxy = initiator.proxyTicket;128if (proxy != null) {129Credentials proxyCreds = Krb5Util.ticketToCreds(proxy);130return new Krb5ProxyCredential(initiator,131Krb5NameElement.getInstance(proxyCreds.getClient()),132proxyCreds.getTicket());133} else {134return initiator;135}136} catch (KrbException | IOException e) {137throw new GSSException(GSSException.DEFECTIVE_CREDENTIAL, -1,138"Cannot create proxy credential");139}140}141}142143144