Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/KrbTgsRep.java
41159 views
/*1* Copyright (c) 2000, 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*/2425/*26*27* (C) Copyright IBM Corp. 1999 All Rights Reserved.28* Copyright 1997 The Open Group Research Institute. All rights reserved.29*/3031package sun.security.krb5;3233import sun.security.krb5.internal.*;34import sun.security.krb5.internal.crypto.KeyUsage;35import sun.security.util.*;36import java.io.IOException;3738/**39* This class encapsulates a TGS-REP that is sent from the KDC to the40* Kerberos client.41*/42public class KrbTgsRep extends KrbKdcRep {43private TGSRep rep;44private Credentials creds;45private Ticket secondTicket;4647KrbTgsRep(byte[] ibuf, KrbTgsReq tgsReq)48throws KrbException, IOException {49DerValue ref = new DerValue(ibuf);50TGSReq req = tgsReq.getMessage();51TGSRep rep = null;52try {53rep = new TGSRep(ref);54} catch (Asn1Exception e) {55rep = null;56KRBError err = new KRBError(ref);57String errStr = err.getErrorString();58String eText = null; // pick up text sent by the server (if any)59if (errStr != null && errStr.length() > 0) {60if (errStr.charAt(errStr.length() - 1) == 0)61eText = errStr.substring(0, errStr.length() - 1);62else63eText = errStr;64}65KrbException ke;66if (eText == null) {67// no text sent from server68ke = new KrbException(err.getErrorCode());69} else {70// override default text with server text71ke = new KrbException(err.getErrorCode(), eText);72}73ke.initCause(e);74throw ke;75}76byte[] enc_tgs_rep_bytes = rep.encPart.decrypt(tgsReq.tgsReqKey,77tgsReq.usedSubkey() ? KeyUsage.KU_ENC_TGS_REP_PART_SUBKEY :78KeyUsage.KU_ENC_TGS_REP_PART_SESSKEY);7980byte[] enc_tgs_rep_part = rep.encPart.reset(enc_tgs_rep_bytes);81ref = new DerValue(enc_tgs_rep_part);82EncTGSRepPart enc_part = new EncTGSRepPart(ref);83rep.encKDCRepPart = enc_part;8485check(false, req, rep, tgsReq.tgsReqKey);8687PrincipalName serverAlias = tgsReq.getServerAlias();88if (serverAlias != null) {89PrincipalName repSname = enc_part.sname;90if (serverAlias.equals(repSname) ||91isReferralSname(repSname)) {92serverAlias = null;93}94}9596PrincipalName clientAlias = null;97if (rep.cname.equals(req.reqBody.cname)) {98// Only propagate the client alias if it is not an99// impersonation ticket (S4U2Self or S4U2Proxy).100clientAlias = tgsReq.getClientAlias();101}102103this.creds = new Credentials(rep.ticket,104rep.cname,105clientAlias,106enc_part.sname,107serverAlias,108enc_part.key,109enc_part.flags,110enc_part.authtime,111enc_part.starttime,112enc_part.endtime,113enc_part.renewTill,114enc_part.caddr115);116this.rep = rep;117this.secondTicket = tgsReq.getSecondTicket();118}119120/**121* Return the credentials that were contained in this KRB-TGS-REP.122*/123public Credentials getCreds() {124return creds;125}126127sun.security.krb5.internal.ccache.Credentials setCredentials() {128return new sun.security.krb5.internal.ccache.Credentials(rep, secondTicket);129}130131private static boolean isReferralSname(PrincipalName sname) {132if (sname != null) {133String[] snameStrings = sname.getNameStrings();134if (snameStrings.length == 2 &&135snameStrings[0].equals(136PrincipalName.TGS_DEFAULT_SRV_NAME)) {137return true;138}139}140return false;141}142}143144145