Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KrbCredInfo.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;3637/**38* Implements the ASN.1 KrbCredInfo type.39*40* <pre>{@code41* KrbCredInfo ::= SEQUENCE {42* key [0] EncryptionKey,43* prealm [1] Realm OPTIONAL,44* pname [2] PrincipalName OPTIONAL,45* flags [3] TicketFlags OPTIONAL,46* authtime [4] KerberosTime OPTIONAL,47* starttime [5] KerberosTime OPTIONAL,48* endtime [6] KerberosTime OPTIONAL,49* renew-till [7] KerberosTime OPTIONAL,50* srealm [8] Realm OPTIONAL,51* sname [9] PrincipalName OPTIONAL,52* caddr [10] HostAddresses OPTIONAL53* }54* }</pre>55*56* <p>57* This definition reflects the Network Working Group RFC 412058* specification available at59* <a href="http://www.ietf.org/rfc/rfc4120.txt">60* http://www.ietf.org/rfc/rfc4120.txt</a>.61*/6263public class KrbCredInfo {64public EncryptionKey key;65public PrincipalName pname; //optional66public TicketFlags flags; //optional67public KerberosTime authtime; //optional68public KerberosTime starttime; //optional69public KerberosTime endtime; //optional70public KerberosTime renewTill; //optional71public PrincipalName sname; //optional72public HostAddresses caddr; //optional7374private KrbCredInfo() {75}7677public KrbCredInfo(78EncryptionKey new_key,79PrincipalName new_pname,80TicketFlags new_flags,81KerberosTime new_authtime,82KerberosTime new_starttime,83KerberosTime new_endtime,84KerberosTime new_renewTill,85PrincipalName new_sname,86HostAddresses new_caddr87) {88key = new_key;89pname = new_pname;90flags = new_flags;91authtime = new_authtime;92starttime = new_starttime;93endtime = new_endtime;94renewTill = new_renewTill;95sname = new_sname;96caddr = new_caddr;97}9899/**100* Constructs a KrbCredInfo object.101* @param encoding a Der-encoded data.102* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.103* @exception IOException if an I/O error occurs while reading encoded data.104* @exception RealmException if an error occurs while parsing a Realm object.105*/106public KrbCredInfo(DerValue encoding)107throws Asn1Exception, IOException, RealmException{108if (encoding.getTag() != DerValue.tag_Sequence) {109throw new Asn1Exception(Krb5.ASN1_BAD_ID);110}111pname = null;112flags = null;113authtime = null;114starttime = null;115endtime = null;116renewTill = null;117sname = null;118caddr = null;119key = EncryptionKey.parse(encoding.getData(), (byte)0x00, false);120Realm prealm = null, srealm = null;121if (encoding.getData().available() > 0)122prealm = Realm.parse(encoding.getData(), (byte)0x01, true);123if (encoding.getData().available() > 0)124pname = PrincipalName.parse(encoding.getData(), (byte)0x02, true, prealm);125if (encoding.getData().available() > 0)126flags = TicketFlags.parse(encoding.getData(), (byte)0x03, true);127if (encoding.getData().available() > 0)128authtime = KerberosTime.parse(encoding.getData(), (byte)0x04, true);129if (encoding.getData().available() > 0)130starttime = KerberosTime.parse(encoding.getData(), (byte)0x05, true);131if (encoding.getData().available() > 0)132endtime = KerberosTime.parse(encoding.getData(), (byte)0x06, true);133if (encoding.getData().available() > 0)134renewTill = KerberosTime.parse(encoding.getData(), (byte)0x07, true);135if (encoding.getData().available() > 0)136srealm = Realm.parse(encoding.getData(), (byte)0x08, true);137if (encoding.getData().available() > 0)138sname = PrincipalName.parse(encoding.getData(), (byte)0x09, true, srealm);139if (encoding.getData().available() > 0)140caddr = HostAddresses.parse(encoding.getData(), (byte)0x0A, true);141if (encoding.getData().available() > 0)142throw new Asn1Exception(Krb5.ASN1_BAD_ID);143}144145/**146* Encodes an KrbCredInfo object.147* @return the byte array of encoded KrbCredInfo object.148* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.149* @exception IOException if an I/O error occurs while reading encoded data.150*/151public byte[] asn1Encode() throws Asn1Exception, IOException {152Vector<DerValue> v = new Vector<>();153v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), key.asn1Encode()));154if (pname != null) {155v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), pname.getRealm().asn1Encode()));156v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), pname.asn1Encode()));157}158if (flags != null)159v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), flags.asn1Encode()));160if (authtime != null)161v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), authtime.asn1Encode()));162if (starttime != null)163v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), starttime.asn1Encode()));164if (endtime != null)165v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x06), endtime.asn1Encode()));166if (renewTill != null)167v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x07), renewTill.asn1Encode()));168if (sname != null) {169v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x08), sname.getRealm().asn1Encode()));170v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x09), sname.asn1Encode()));171}172if (caddr != null)173v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x0A), caddr.asn1Encode()));174DerValue[] der = new DerValue[v.size()];175v.copyInto(der);176DerOutputStream out = new DerOutputStream();177out.putSequence(der);178return out.toByteArray();179}180181public Object clone() {182KrbCredInfo kcred = new KrbCredInfo();183kcred.key = (EncryptionKey)key.clone();184// optional fields185if (pname != null)186kcred.pname = (PrincipalName)pname.clone();187if (flags != null)188kcred.flags = (TicketFlags)flags.clone();189kcred.authtime = authtime;190kcred.starttime = starttime;191kcred.endtime = endtime;192kcred.renewTill = renewTill;193if (sname != null)194kcred.sname = (PrincipalName)sname.clone();195if (caddr != null)196kcred.caddr = (HostAddresses)caddr.clone();197return kcred;198}199200}201202203