Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/wrapper/GSSCredElement.java
41161 views
/*1* Copyright (c) 2005, 2021, 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*/24package sun.security.jgss.wrapper;2526import org.ietf.jgss.*;27import java.security.Provider;28import sun.security.jgss.GSSUtil;29import sun.security.jgss.spi.GSSCredentialSpi;30import sun.security.jgss.spi.GSSNameSpi;3132/**33* This class is essentially a wrapper class for the gss_cred_id_t34* structure of the native GSS library.35* @author Valerie Peng36* @since 1.637*/38public class GSSCredElement implements GSSCredentialSpi {3940private int usage;41long pCred; // Pointer to the gss_cred_id_t structure42private GSSNameElement name = null;43private GSSLibStub cStub;4445// Perform the necessary ServicePermission check on this cred46@SuppressWarnings("removal")47void doServicePermCheck() throws GSSException {48if (GSSUtil.isKerberosMech(cStub.getMech())) {49if (System.getSecurityManager() != null) {50if (isInitiatorCredential()) {51String tgsName = Krb5Util.getTGSName(name);52Krb5Util.checkServicePermission(tgsName, "initiate");53}54if (isAcceptorCredential() &&55name != GSSNameElement.DEF_ACCEPTOR) {56String krbName = name.getKrbName();57Krb5Util.checkServicePermission(krbName, "accept");58}59}60}61}6263// Construct delegation cred using the actual context mech and srcName64// Warning: called by NativeUtil.c65GSSCredElement(long pCredentials, GSSNameElement srcName, Oid mech)66throws GSSException {67pCred = pCredentials;68cStub = GSSLibStub.getInstance(mech);69usage = GSSCredential.INITIATE_ONLY;70name = srcName;71}7273GSSCredElement(GSSNameElement name, int lifetime, int usage,74GSSLibStub stub) throws GSSException {75cStub = stub;76this.usage = usage;7778if (name != null) { // Could be GSSNameElement.DEF_ACCEPTOR79this.name = name;80doServicePermCheck();81pCred = cStub.acquireCred(this.name.pName, lifetime, usage);82} else {83pCred = cStub.acquireCred(0, lifetime, usage);84this.name = new GSSNameElement(cStub.getCredName(pCred), cStub);85doServicePermCheck();86}87}8889public Provider getProvider() {90return SunNativeProvider.INSTANCE;91}9293public void dispose() throws GSSException {94name = null;95if (pCred != 0) {96pCred = cStub.releaseCred(pCred);97}98}99100public GSSNameElement getName() throws GSSException {101return (name == GSSNameElement.DEF_ACCEPTOR ?102null : name);103}104105public int getInitLifetime() throws GSSException {106if (isInitiatorCredential()) {107return cStub.getCredTime(pCred);108} else return 0;109}110111public int getAcceptLifetime() throws GSSException {112if (isAcceptorCredential()) {113return cStub.getCredTime(pCred);114} else return 0;115}116117public boolean isInitiatorCredential() {118return (usage != GSSCredential.ACCEPT_ONLY);119}120121public boolean isAcceptorCredential() {122return (usage != GSSCredential.INITIATE_ONLY);123}124125public Oid getMechanism() {126return cStub.getMech();127}128129public String toString() {130// No hex bytes available for native impl131return "N/A";132}133134@SuppressWarnings("deprecation")135protected void finalize() throws Throwable {136dispose();137}138139@Override140public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {141throw new GSSException(GSSException.FAILURE, -1,142"Not supported yet");143}144}145146147