0N/A/*
2551N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A * (C) Copyright IBM Corp. 1999 All Rights Reserved.
0N/A * Copyright 1997 The Open Group Research Institute. All rights reserved.
0N/A */
0N/A
0N/Apackage sun.security.krb5;
0N/A
0N/Aimport sun.security.krb5.internal.*;
0N/Aimport sun.security.krb5.internal.crypto.KeyUsage;
0N/Aimport sun.security.util.*;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class encapsulates a KRB-AP-REP sent from the service to the
0N/A * client.
0N/A */
0N/Apublic class KrbApRep {
0N/A private byte[] obuf;
0N/A private byte[] ibuf;
0N/A private EncAPRepPart encPart; // although in plain text
0N/A private APRep apRepMessg;
0N/A
0N/A /**
0N/A * Constructs a KRB-AP-REP to send to a client.
0N/A * @throws KrbException
0N/A * @throws IOException
0N/A */
0N/A // Used in AcceptSecContextToken
0N/A public KrbApRep(KrbApReq incomingReq,
0N/A boolean useSeqNumber,
0N/A boolean useSubKey) throws KrbException, IOException {
0N/A
0N/A EncryptionKey subKey =
0N/A (useSubKey?
0N/A new EncryptionKey(incomingReq.getCreds().getSessionKey()):null);
0N/A SeqNumber seqNum = new LocalSeqNumber();
0N/A
0N/A init(incomingReq, subKey, seqNum);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a KRB-AP-REQ from the bytes received from a service.
0N/A * @throws KrbException
0N/A * @throws IOException
0N/A */
0N/A // Used in AcceptSecContextToken
0N/A public KrbApRep(byte[] message, Credentials tgtCreds,
0N/A KrbApReq outgoingReq) throws KrbException, IOException {
0N/A this(message, tgtCreds);
0N/A authenticate(outgoingReq);
0N/A }
0N/A
0N/A private void init(KrbApReq apReq,
0N/A EncryptionKey subKey,
0N/A SeqNumber seqNumber)
0N/A throws KrbException, IOException {
0N/A createMessage(
0N/A apReq.getCreds().key,
0N/A apReq.getCtime(),
0N/A apReq.cusec(),
0N/A subKey,
0N/A seqNumber);
0N/A obuf = apRepMessg.asn1Encode();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a KrbApRep object.
0N/A * @param msg a byte array of reply message.
0N/A * @param tgs_creds client's credential.
0N/A * @exception KrbException
0N/A * @exception IOException
0N/A */
0N/A private KrbApRep(byte[] msg, Credentials tgs_creds)
0N/A throws KrbException, IOException {
0N/A this(new DerValue(msg), tgs_creds);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a KrbApRep object.
0N/A * @param msg a byte array of reply message.
0N/A * @param tgs_creds client's credential.
0N/A * @exception KrbException
0N/A * @exception IOException
0N/A */
0N/A private KrbApRep(DerValue encoding, Credentials tgs_creds)
0N/A throws KrbException, IOException {
0N/A APRep rep = null;
0N/A try {
0N/A rep = new APRep(encoding);
0N/A } catch (Asn1Exception e) {
0N/A rep = null;
0N/A KRBError err = new KRBError(encoding);
0N/A String errStr = err.getErrorString();
0N/A String eText;
0N/A if (errStr.charAt(errStr.length() - 1) == 0)
0N/A eText = errStr.substring(0, errStr.length() - 1);
0N/A else
0N/A eText = errStr;
0N/A KrbException ke = new KrbException(err.getErrorCode(), eText);
0N/A ke.initCause(e);
0N/A throw ke;
0N/A }
0N/A
0N/A byte[] temp = rep.encPart.decrypt(tgs_creds.key,
0N/A KeyUsage.KU_ENC_AP_REP_PART);
2551N/A byte[] enc_ap_rep_part = rep.encPart.reset(temp);
0N/A
0N/A encoding = new DerValue(enc_ap_rep_part);
0N/A encPart = new EncAPRepPart(encoding);
0N/A }
0N/A
0N/A private void authenticate(KrbApReq apReq)
0N/A throws KrbException, IOException {
0N/A if (encPart.ctime.getSeconds() != apReq.getCtime().getSeconds() ||
0N/A encPart.cusec != apReq.getCtime().getMicroSeconds())
0N/A throw new KrbApErrException(Krb5.KRB_AP_ERR_MUT_FAIL);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the optional subkey stored in
0N/A * this message. Returns null if none is stored.
0N/A */
0N/A public EncryptionKey getSubKey() {
0N/A // XXX Can encPart be null
0N/A return encPart.getSubKey();
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns the optional sequence number stored in the
0N/A * this message. Returns null if none is stored.
0N/A */
0N/A public Integer getSeqNumber() {
0N/A // XXX Can encPart be null
0N/A return encPart.getSeqNumber();
0N/A }
0N/A
0N/A /**
0N/A * Returns the ASN.1 encoding that should be sent to the peer.
0N/A */
0N/A public byte[] getMessage() {
0N/A return obuf;
0N/A }
0N/A
0N/A private void createMessage(
0N/A EncryptionKey key,
0N/A KerberosTime ctime,
0N/A int cusec,
0N/A EncryptionKey subKey,
0N/A SeqNumber seqNumber)
0N/A throws Asn1Exception, IOException,
0N/A KdcErrException, KrbCryptoException {
0N/A
0N/A Integer seqno = null;
0N/A
0N/A if (seqNumber != null)
0N/A seqno = new Integer(seqNumber.current());
0N/A
0N/A encPart = new EncAPRepPart(ctime,
0N/A cusec,
0N/A subKey,
0N/A seqno);
0N/A
0N/A byte[] encPartEncoding = encPart.asn1Encode();
0N/A
0N/A EncryptedData encEncPart = new EncryptedData(key, encPartEncoding,
0N/A KeyUsage.KU_ENC_AP_REP_PART);
0N/A
0N/A apRepMessg = new APRep(encEncPart);
0N/A }
0N/A
0N/A}