Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KDCRep.java
41161 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25*26* (C) Copyright IBM Corp. 1999 All Rights Reserved.27* Copyright 1997 The Open Group Research Institute. All rights reserved.28*/2930package sun.security.krb5.internal;3132import sun.security.krb5.*;33import sun.security.util.*;34import java.io.IOException;35import java.math.BigInteger;3637/**38* Implements the ASN.1 KDC-REP type.39*40* <pre>{@code41* KDC-REP ::= SEQUENCE {42* pvno [0] INTEGER (5),43* msg-type [1] INTEGER (11 -- AS -- | 13 -- TGS --),44* padata [2] SEQUENCE OF PA-DATA OPTIONAL45* -- NOTE: not empty --,46* crealm [3] Realm,47* cname [4] PrincipalName,48* ticket [5] Ticket,49* enc-part [6] EncryptedData50* -- EncASRepPart or EncTGSRepPart,51* -- as appropriate52* }53* }</pre>54*55* <p>56* This definition reflects the Network Working Group RFC 412057* specification available at58* <a href="http://www.ietf.org/rfc/rfc4120.txt">59* http://www.ietf.org/rfc/rfc4120.txt</a>.60*/61public class KDCRep {6263public PrincipalName cname;64public Ticket ticket;65public EncryptedData encPart;66public EncKDCRepPart encKDCRepPart; //not part of ASN.1 encoding67private int pvno;68private int msgType;69public PAData[] pAData = null; //optional70private boolean DEBUG = Krb5.DEBUG;7172public KDCRep(73PAData[] new_pAData,74PrincipalName new_cname,75Ticket new_ticket,76EncryptedData new_encPart,77int req_type) throws IOException {78pvno = Krb5.PVNO;79msgType = req_type;80if (new_pAData != null) {81pAData = new PAData[new_pAData.length];82for (int i = 0; i < new_pAData.length; i++) {83if (new_pAData[i] == null) {84throw new IOException("Cannot create a KDCRep");85} else {86pAData[i] = (PAData) new_pAData[i].clone();87}88}89}90cname = new_cname;91ticket = new_ticket;92encPart = new_encPart;93}9495public KDCRep() {96}9798public KDCRep(byte[] data, int req_type) throws Asn1Exception,99KrbApErrException, RealmException, IOException {100init(new DerValue(data), req_type);101}102103public KDCRep(DerValue encoding, int req_type) throws Asn1Exception,104RealmException, KrbApErrException, IOException {105init(encoding, req_type);106}107108/*109// Not used? Don't know what keyusage to use here %%%110public void decrypt(EncryptionKey key) throws Asn1Exception,111IOException, KrbException, RealmException {112encKDCRepPart = new EncKDCRepPart(encPart.decrypt(key), msgType);113}114*/115/**116* Initializes an KDCRep object.117*118* @param encoding a single DER-encoded value.119* @param req_type reply message type.120* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.121* @exception IOException if an I/O error occurs while reading encoded data.122* @exception RealmException if an error occurs while constructing123* a Realm object from DER-encoded data.124* @exception KrbApErrException if the value read from the DER-encoded125* data stream does not match the pre-defined value.126*127*/128protected void init(DerValue encoding, int req_type)129throws Asn1Exception, RealmException, IOException,130KrbApErrException {131DerValue der, subDer;132if ((encoding.getTag() & 0x1F) != req_type) {133if (DEBUG) {134System.out.println(">>> KDCRep: init() " +135"encoding tag is " +136encoding.getTag() +137" req type is " + req_type);138}139throw new Asn1Exception(Krb5.ASN1_BAD_ID);140}141der = encoding.getData().getDerValue();142if (der.getTag() != DerValue.tag_Sequence) {143throw new Asn1Exception(Krb5.ASN1_BAD_ID);144}145subDer = der.getData().getDerValue();146if ((subDer.getTag() & 0x1F) == 0x00) {147pvno = subDer.getData().getBigInteger().intValue();148if (pvno != Krb5.PVNO) {149throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);150}151} else {152throw new Asn1Exception(Krb5.ASN1_BAD_ID);153}154subDer = der.getData().getDerValue();155if ((subDer.getTag() & 0x1F) == 0x01) {156msgType = subDer.getData().getBigInteger().intValue();157if (msgType != req_type) {158throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);159}160} else {161throw new Asn1Exception(Krb5.ASN1_BAD_ID);162}163if ((der.getData().peekByte() & 0x1F) == 0x02) {164subDer = der.getData().getDerValue();165DerValue[] padata = subDer.getData().getSequence(1);166pAData = new PAData[padata.length];167for (int i = 0; i < padata.length; i++) {168pAData[i] = new PAData(padata[i]);169}170} else {171pAData = null;172}173Realm crealm = Realm.parse(der.getData(), (byte) 0x03, false);174cname = PrincipalName.parse(der.getData(), (byte) 0x04, false, crealm);175ticket = Ticket.parse(der.getData(), (byte) 0x05, false);176encPart = EncryptedData.parse(der.getData(), (byte) 0x06, false);177if (der.getData().available() > 0) {178throw new Asn1Exception(Krb5.ASN1_BAD_ID);179}180}181182/**183* Encodes this object to a byte array.184* @return byte array of encoded APReq object.185* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.186* @exception IOException if an I/O error occurs while reading encoded data.187*188*/189public byte[] asn1Encode() throws Asn1Exception, IOException {190191DerOutputStream bytes = new DerOutputStream();192DerOutputStream temp = new DerOutputStream();193temp.putInteger(BigInteger.valueOf(pvno));194bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,195true, (byte) 0x00), temp);196temp = new DerOutputStream();197temp.putInteger(BigInteger.valueOf(msgType));198bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,199true, (byte) 0x01), temp);200if (pAData != null && pAData.length > 0) {201DerOutputStream padata_stream = new DerOutputStream();202for (int i = 0; i < pAData.length; i++) {203padata_stream.write(pAData[i].asn1Encode());204}205temp = new DerOutputStream();206temp.write(DerValue.tag_SequenceOf, padata_stream);207bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,208true, (byte) 0x02), temp);209}210bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,211true, (byte) 0x03), cname.getRealm().asn1Encode());212bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,213true, (byte) 0x04), cname.asn1Encode());214bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,215true, (byte) 0x05), ticket.asn1Encode());216bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,217true, (byte) 0x06), encPart.asn1Encode());218temp = new DerOutputStream();219temp.write(DerValue.tag_Sequence, bytes);220return temp.toByteArray();221}222}223224225