Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/krb5/Krb5Util.java
41161 views
/*1* Copyright (c) 2003, 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*/2425package sun.security.jgss.krb5;2627import javax.security.auth.kerberos.KerberosTicket;28import javax.security.auth.kerberos.KerberosPrincipal;29import javax.security.auth.kerberos.KeyTab;30import javax.security.auth.Subject;31import javax.security.auth.login.LoginException;32import java.security.AccessControlContext;3334import sun.security.action.GetBooleanAction;35import sun.security.jgss.GSSUtil;36import sun.security.jgss.GSSCaller;3738import sun.security.krb5.Credentials;39import sun.security.krb5.EncryptionKey;40import sun.security.krb5.KrbException;41import java.io.IOException;42import sun.security.krb5.KerberosSecrets;43import sun.security.krb5.PrincipalName;4445/**46* Utilities for obtaining and converting Kerberos tickets.47*/48public class Krb5Util {4950static final boolean DEBUG = GetBooleanAction51.privilegedGetProperty("sun.security.krb5.debug");5253/**54* Default constructor55*/56private Krb5Util() { // Cannot create one of these57}5859/**60* Retrieves the ticket corresponding to the client/server principal61* pair from the Subject in the specified AccessControlContext.62*/63static KerberosTicket getServiceTicket(GSSCaller caller,64String clientPrincipal, String serverPrincipal,65@SuppressWarnings("removal") AccessControlContext acc) throws LoginException {6667// Try to get ticket from acc's Subject68@SuppressWarnings("removal")69Subject accSubj = Subject.getSubject(acc);70KerberosTicket ticket =71SubjectComber.find(accSubj, serverPrincipal, clientPrincipal,72KerberosTicket.class);7374return ticket;75}7677/**78* Retrieves the initial TGT corresponding to the client principal79* from the Subject in the specified AccessControlContext.80* If the ticket can not be found in the Subject, and if81* useSubjectCredsOnly is false, then obtain ticket from82* a LoginContext.83*/84static KerberosTicket getInitialTicket(GSSCaller caller,85String clientPrincipal,86@SuppressWarnings("removal") AccessControlContext acc) throws LoginException {8788// Try to get ticket from acc's Subject89@SuppressWarnings("removal")90Subject accSubj = Subject.getSubject(acc);91KerberosTicket ticket =92SubjectComber.find(accSubj, null, clientPrincipal,93KerberosTicket.class);9495// Try to get ticket from Subject obtained from GSSUtil96if (ticket == null && !GSSUtil.useSubjectCredsOnly(caller)) {97Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID);98ticket = SubjectComber.find(subject,99null, clientPrincipal, KerberosTicket.class);100}101return ticket;102}103104/**105* Retrieves the ServiceCreds for the specified server principal from106* the Subject in the specified AccessControlContext. If not found, and if107* useSubjectCredsOnly is false, then obtain from a LoginContext.108*109* NOTE: This method is also used by JSSE Kerberos Cipher Suites110*/111public static ServiceCreds getServiceCreds(GSSCaller caller,112String serverPrincipal, @SuppressWarnings("removal") AccessControlContext acc)113throws LoginException {114115@SuppressWarnings("removal")116Subject accSubj = Subject.getSubject(acc);117ServiceCreds sc = null;118if (accSubj != null) {119sc = ServiceCreds.getInstance(accSubj, serverPrincipal);120}121if (sc == null && !GSSUtil.useSubjectCredsOnly(caller)) {122Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID);123sc = ServiceCreds.getInstance(subject, serverPrincipal);124}125return sc;126}127128public static KerberosTicket credsToTicket(Credentials serviceCreds) {129EncryptionKey sessionKey = serviceCreds.getSessionKey();130KerberosTicket kt = new KerberosTicket(131serviceCreds.getEncoded(),132new KerberosPrincipal(serviceCreds.getClient().getName()),133new KerberosPrincipal(serviceCreds.getServer().getName(),134KerberosPrincipal.KRB_NT_SRV_INST),135sessionKey.getBytes(),136sessionKey.getEType(),137serviceCreds.getFlags(),138serviceCreds.getAuthTime(),139serviceCreds.getStartTime(),140serviceCreds.getEndTime(),141serviceCreds.getRenewTill(),142serviceCreds.getClientAddresses());143PrincipalName clientAlias = serviceCreds.getClientAlias();144PrincipalName serverAlias = serviceCreds.getServerAlias();145if (clientAlias != null) {146KerberosSecrets.getJavaxSecurityAuthKerberosAccess()147.kerberosTicketSetClientAlias(kt, new KerberosPrincipal(148clientAlias.getName(), clientAlias.getNameType()));149}150if (serverAlias != null) {151KerberosSecrets.getJavaxSecurityAuthKerberosAccess()152.kerberosTicketSetServerAlias(kt, new KerberosPrincipal(153serverAlias.getName(), serverAlias.getNameType()));154}155return kt;156};157158public static Credentials ticketToCreds(KerberosTicket kerbTicket)159throws KrbException, IOException {160KerberosPrincipal clientAlias = KerberosSecrets161.getJavaxSecurityAuthKerberosAccess()162.kerberosTicketGetClientAlias(kerbTicket);163KerberosPrincipal serverAlias = KerberosSecrets164.getJavaxSecurityAuthKerberosAccess()165.kerberosTicketGetServerAlias(kerbTicket);166return new Credentials(167kerbTicket.getEncoded(),168kerbTicket.getClient().getName(),169(clientAlias != null ? clientAlias.getName() : null),170kerbTicket.getServer().getName(),171(serverAlias != null ? serverAlias.getName() : null),172kerbTicket.getSessionKey().getEncoded(),173kerbTicket.getSessionKeyType(),174kerbTicket.getFlags(),175kerbTicket.getAuthTime(),176kerbTicket.getStartTime(),177kerbTicket.getEndTime(),178kerbTicket.getRenewTill(),179kerbTicket.getClientAddresses());180}181182/**183* A helper method to get a sun..KeyTab from a javax..KeyTab184* @param ktab the javax..KeyTab object185* @return the sun..KeyTab object186*/187public static sun.security.krb5.internal.ktab.KeyTab188snapshotFromJavaxKeyTab(KeyTab ktab) {189return KerberosSecrets.getJavaxSecurityAuthKerberosAccess()190.keyTabTakeSnapshot(ktab);191}192193/**194* A helper method to get EncryptionKeys from a javax..KeyTab195* @param ktab the javax..KeyTab object196* @param cname the PrincipalName197* @return the EKeys, never null, might be empty198*/199public static EncryptionKey[] keysFromJavaxKeyTab(200KeyTab ktab, PrincipalName cname) {201return snapshotFromJavaxKeyTab(ktab).readServiceKeys(cname);202}203}204205206