Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/EncKDCRepPart.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.util.Vector;35import java.io.IOException;36import java.math.BigInteger;3738/**39* Implements the ASN.1 EncKDCRepPart type.40*41* <pre>{@code42* EncKDCRepPart ::= SEQUENCE {43* key [0] EncryptionKey,44* last-req [1] LastReq,45* nonce [2] UInt32,46* key-expiration [3] KerberosTime OPTIONAL,47* flags [4] TicketFlags,48* authtime [5] KerberosTime,49* starttime [6] KerberosTime OPTIONAL,50* endtime [7] KerberosTime,51* renew-till [8] KerberosTime OPTIONAL,52* srealm [9] Realm,53* sname [10] PrincipalName,54* caddr [11] HostAddresses OPTIONAL,55* encrypted-pa-data [12] SEQUENCE OF PA-DATA OPTIONAL56* }57* }</pre>58*59* <p>60* This definition reflects the Network Working Group RFC 412061* specification available at62* <a href="http://www.ietf.org/rfc/rfc4120.txt">63* http://www.ietf.org/rfc/rfc4120.txt</a>.64*/65public class EncKDCRepPart {6667public EncryptionKey key;68public LastReq lastReq;69public int nonce;70public KerberosTime keyExpiration; //optional71public TicketFlags flags;72public KerberosTime authtime;73public KerberosTime starttime; //optional74public KerberosTime endtime;75public KerberosTime renewTill; //optional76public PrincipalName sname;77public HostAddresses caddr; //optional78public PAData[] pAData; //optional79public int msgType; //not included in sequence8081public EncKDCRepPart(82EncryptionKey new_key,83LastReq new_lastReq,84int new_nonce,85KerberosTime new_keyExpiration,86TicketFlags new_flags,87KerberosTime new_authtime,88KerberosTime new_starttime,89KerberosTime new_endtime,90KerberosTime new_renewTill,91PrincipalName new_sname,92HostAddresses new_caddr,93PAData[] new_pAData,94int new_msgType) {95key = new_key;96lastReq = new_lastReq;97nonce = new_nonce;98keyExpiration = new_keyExpiration;99flags = new_flags;100authtime = new_authtime;101starttime = new_starttime;102endtime = new_endtime;103renewTill = new_renewTill;104sname = new_sname;105caddr = new_caddr;106pAData = new_pAData;107msgType = new_msgType;108}109110public EncKDCRepPart() {111}112113public EncKDCRepPart(byte[] data, int rep_type)114throws Asn1Exception, IOException, RealmException {115init(new DerValue(data), rep_type);116}117118public EncKDCRepPart(DerValue encoding, int rep_type)119throws Asn1Exception, IOException, RealmException {120init(encoding, rep_type);121}122123/**124* Initializes an EncKDCRepPart object.125*126* @param encoding a single DER-encoded value.127* @param rep_type type of the encrypted reply message.128* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.129* @exception IOException if an I/O error occurs while reading encoded data.130* @exception RealmException if an error occurs while decoding an Realm object.131*/132protected void init(DerValue encoding, int rep_type)133throws Asn1Exception, IOException, RealmException {134DerValue der, subDer;135//implementations return the incorrect tag value, so136//we don't use the above line; instead we use the following137msgType = (encoding.getTag() & (byte) 0x1F);138if (msgType != Krb5.KRB_ENC_AS_REP_PART &&139msgType != Krb5.KRB_ENC_TGS_REP_PART) {140throw new Asn1Exception(Krb5.ASN1_BAD_ID);141}142der = encoding.getData().getDerValue();143if (der.getTag() != DerValue.tag_Sequence) {144throw new Asn1Exception(Krb5.ASN1_BAD_ID);145}146key = EncryptionKey.parse(der.getData(), (byte) 0x00, false);147lastReq = LastReq.parse(der.getData(), (byte) 0x01, false);148subDer = der.getData().getDerValue();149if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x02) {150nonce = subDer.getData().getBigInteger().intValue();151} else {152throw new Asn1Exception(Krb5.ASN1_BAD_ID);153}154keyExpiration = KerberosTime.parse(der.getData(), (byte) 0x03, true);155flags = TicketFlags.parse(der.getData(), (byte) 0x04, false);156authtime = KerberosTime.parse(der.getData(), (byte) 0x05, false);157starttime = KerberosTime.parse(der.getData(), (byte) 0x06, true);158endtime = KerberosTime.parse(der.getData(), (byte) 0x07, false);159renewTill = KerberosTime.parse(der.getData(), (byte) 0x08, true);160Realm srealm = Realm.parse(der.getData(), (byte) 0x09, false);161sname = PrincipalName.parse(der.getData(), (byte) 0x0A, false, srealm);162if (der.getData().available() > 0) {163caddr = HostAddresses.parse(der.getData(), (byte) 0x0B, true);164}165if (der.getData().available() > 0) {166pAData = PAData.parseSequence(der.getData(), (byte) 0x0C, true);167}168// We observe extra data from MSAD169/*if (der.getData().available() > 0) {170throw new Asn1Exception(Krb5.ASN1_BAD_ID);171}*/172}173174/**175* Encodes an EncKDCRepPart object.176* @param rep_type type of encrypted reply message.177* @return byte array of encoded EncKDCRepPart object.178* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.179* @exception IOException if an I/O error occurs while reading encoded data.180*/181public byte[] asn1Encode(int rep_type) throws Asn1Exception,182IOException {183DerOutputStream bytes;184DerOutputStream temp = new DerOutputStream();185DerOutputStream out = new DerOutputStream();186out.write(DerValue.createTag(DerValue.TAG_CONTEXT,187true, (byte) 0x00), key.asn1Encode());188out.write(DerValue.createTag(DerValue.TAG_CONTEXT,189true, (byte) 0x01), lastReq.asn1Encode());190temp.putInteger(BigInteger.valueOf(nonce));191out.write(DerValue.createTag(DerValue.TAG_CONTEXT,192true, (byte) 0x02), temp);193194if (keyExpiration != null) {195out.write(DerValue.createTag(DerValue.TAG_CONTEXT,196true, (byte) 0x03), keyExpiration.asn1Encode());197}198out.write(DerValue.createTag(DerValue.TAG_CONTEXT,199true, (byte) 0x04), flags.asn1Encode());200out.write(DerValue.createTag(DerValue.TAG_CONTEXT,201true, (byte) 0x05), authtime.asn1Encode());202if (starttime != null) {203out.write(DerValue.createTag(DerValue.TAG_CONTEXT,204true, (byte) 0x06), starttime.asn1Encode());205}206out.write(DerValue.createTag(DerValue.TAG_CONTEXT,207true, (byte) 0x07), endtime.asn1Encode());208if (renewTill != null) {209out.write(DerValue.createTag(DerValue.TAG_CONTEXT,210true, (byte) 0x08), renewTill.asn1Encode());211}212out.write(DerValue.createTag(DerValue.TAG_CONTEXT,213true, (byte) 0x09), sname.getRealm().asn1Encode());214out.write(DerValue.createTag(DerValue.TAG_CONTEXT,215true, (byte) 0x0A), sname.asn1Encode());216if (caddr != null) {217out.write(DerValue.createTag(DerValue.TAG_CONTEXT,218true, (byte) 0x0B), caddr.asn1Encode());219}220if (pAData != null && pAData.length > 0) {221temp = new DerOutputStream();222for (int i = 0; i < pAData.length; i++) {223temp.write(pAData[i].asn1Encode());224}225bytes = new DerOutputStream();226bytes.write(DerValue.tag_SequenceOf, temp);227out.write(DerValue.createTag(DerValue.TAG_CONTEXT,228true, (byte) 0x0C), bytes);229}230//should use the rep_type to build the encoding231//but other implementations do not; it is ignored and232//the cached msgType is used instead233temp = new DerOutputStream();234temp.write(DerValue.tag_Sequence, out);235bytes = new DerOutputStream();236bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION,237true, (byte) msgType), temp);238return bytes.toByteArray();239}240}241242243