Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/EncTicketPart.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.io.*;3738/**39* Implements the ASN.1 EncTicketPart type.40*41* <pre>{@code42* EncTicketPart ::= [APPLICATION 3] SEQUENCE {43* flags [0] TicketFlags,44* key [1] EncryptionKey,45* crealm [2] Realm,46* cname [3] PrincipalName,47* transited [4] TransitedEncoding,48* authtime [5] KerberosTime,49* starttime [6] KerberosTime OPTIONAL,50* endtime [7] KerberosTime,51* renew-till [8] KerberosTime OPTIONAL,52* caddr [9] HostAddresses OPTIONAL,53* authorization-data [10] AuthorizationData OPTIONAL54* }55* }</pre>56*57* <p>58* This definition reflects the Network Working Group RFC 412059* specification available at60* <a href="http://www.ietf.org/rfc/rfc4120.txt">61* http://www.ietf.org/rfc/rfc4120.txt</a>.62*/63public class EncTicketPart {6465public TicketFlags flags;66public EncryptionKey key;67public PrincipalName cname;68public TransitedEncoding transited;69public KerberosTime authtime;70public KerberosTime starttime; //optional71public KerberosTime endtime;72public KerberosTime renewTill; //optional73public HostAddresses caddr; //optional74public AuthorizationData authorizationData; //optional7576public EncTicketPart(77TicketFlags new_flags,78EncryptionKey new_key,79PrincipalName new_cname,80TransitedEncoding new_transited,81KerberosTime new_authtime,82KerberosTime new_starttime,83KerberosTime new_endtime,84KerberosTime new_renewTill,85HostAddresses new_caddr,86AuthorizationData new_authorizationData) {87flags = new_flags;88key = new_key;89cname = new_cname;90transited = new_transited;91authtime = new_authtime;92starttime = new_starttime;93endtime = new_endtime;94renewTill = new_renewTill;95caddr = new_caddr;96authorizationData = new_authorizationData;97}9899public EncTicketPart(byte[] data)100throws Asn1Exception, KrbException, IOException {101init(new DerValue(data));102}103104public EncTicketPart(DerValue encoding)105throws Asn1Exception, KrbException, IOException {106init(encoding);107}108109/**110* Initializes an EncTicketPart object.111* @param encoding a single DER-encoded value.112* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.113* @exception IOException if an I/O error occurs while reading encoded data.114* @exception RealmException if an error occurs while parsing a Realm object.115*/116private static String getHexBytes(byte[] bytes, int len)117throws IOException {118119StringBuilder sb = new StringBuilder();120for (int i = 0; i < len; i++) {121122int b1 = (bytes[i] >> 4) & 0x0f;123int b2 = bytes[i] & 0x0f;124125sb.append(Integer.toHexString(b1));126sb.append(Integer.toHexString(b2));127sb.append(' ');128}129return sb.toString();130}131132private void init(DerValue encoding)133throws Asn1Exception, IOException, RealmException {134DerValue der, subDer;135136renewTill = null;137caddr = null;138authorizationData = null;139if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x03)140|| (encoding.isApplication() != true)141|| (encoding.isConstructed() != true)) {142throw new Asn1Exception(Krb5.ASN1_BAD_ID);143}144der = encoding.getData().getDerValue();145if (der.getTag() != DerValue.tag_Sequence) {146throw new Asn1Exception(Krb5.ASN1_BAD_ID);147}148flags = TicketFlags.parse(der.getData(), (byte) 0x00, false);149key = EncryptionKey.parse(der.getData(), (byte) 0x01, false);150Realm crealm = Realm.parse(der.getData(), (byte) 0x02, false);151cname = PrincipalName.parse(der.getData(), (byte) 0x03, false, crealm);152transited = TransitedEncoding.parse(der.getData(), (byte) 0x04, false);153authtime = KerberosTime.parse(der.getData(), (byte) 0x05, false);154starttime = KerberosTime.parse(der.getData(), (byte) 0x06, true);155endtime = KerberosTime.parse(der.getData(), (byte) 0x07, false);156if (der.getData().available() > 0) {157renewTill = KerberosTime.parse(der.getData(), (byte) 0x08, true);158}159if (der.getData().available() > 0) {160caddr = HostAddresses.parse(der.getData(), (byte) 0x09, true);161}162if (der.getData().available() > 0) {163authorizationData = AuthorizationData.parse(der.getData(), (byte) 0x0A, true);164}165if (der.getData().available() > 0) {166throw new Asn1Exception(Krb5.ASN1_BAD_ID);167}168169}170171/**172* Encodes an EncTicketPart object.173* @return byte array of encoded EncTicketPart object.174* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.175* @exception IOException if an I/O error occurs while reading encoded data.176*/177public byte[] asn1Encode() throws Asn1Exception, IOException {178DerOutputStream bytes = new DerOutputStream();179DerOutputStream temp = new DerOutputStream();180bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,181true, (byte) 0x00), flags.asn1Encode());182bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,183true, (byte) 0x01), key.asn1Encode());184bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,185true, (byte) 0x02), cname.getRealm().asn1Encode());186bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,187true, (byte) 0x03), cname.asn1Encode());188bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,189true, (byte) 0x04), transited.asn1Encode());190bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,191true, (byte) 0x05), authtime.asn1Encode());192if (starttime != null) {193bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,194true, (byte) 0x06), starttime.asn1Encode());195}196bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,197true, (byte) 0x07), endtime.asn1Encode());198199if (renewTill != null) {200bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,201true, (byte) 0x08), renewTill.asn1Encode());202}203204if (caddr != null) {205bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,206true, (byte) 0x09), caddr.asn1Encode());207}208209if (authorizationData != null) {210bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,211true, (byte) 0x0A), authorizationData.asn1Encode());212}213temp.write(DerValue.tag_Sequence, bytes);214bytes = new DerOutputStream();215bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION,216true, (byte) 0x03), temp);217return bytes.toByteArray();218}219}220221222