Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/EncKrbCredPart.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.util.*;33import sun.security.krb5.Asn1Exception;34import sun.security.krb5.RealmException;35import java.util.Vector;36import java.io.IOException;37import java.math.BigInteger;3839/**40* Implements the ASN.1 EncKrbCredPart type.41*42* <pre>{@code43* EncKrbCredPart ::= [APPLICATION 29] SEQUENCE {44* ticket-info [0] SEQUENCE OF KrbCredInfo,45* nonce [1] UInt32 OPTIONAL,46* timestamp [2] KerberosTime OPTIONAL,47* usec [3] Microseconds OPTIONAL,48* s-address [4] HostAddress OPTIONAL,49* r-address [5] HostAddress OPTIONAL50* }51* }</pre>52*53* <p>54* This definition reflects the Network Working Group RFC 412055* specification available at56* <a href="http://www.ietf.org/rfc/rfc4120.txt">57* http://www.ietf.org/rfc/rfc4120.txt</a>.58*/59public class EncKrbCredPart {6061public KrbCredInfo[] ticketInfo = null;62public KerberosTime timeStamp; //optional63private Integer nonce; //optional64private Integer usec; //optional65private HostAddress sAddress; //optional66private HostAddresses rAddress; //optional6768public EncKrbCredPart(69KrbCredInfo[] new_ticketInfo,70KerberosTime new_timeStamp,71Integer new_usec,72Integer new_nonce,73HostAddress new_sAddress,74HostAddresses new_rAddress) throws IOException {75if (new_ticketInfo != null) {76ticketInfo = new KrbCredInfo[new_ticketInfo.length];77for (int i = 0; i < new_ticketInfo.length; i++) {78if (new_ticketInfo[i] == null) {79throw new IOException("Cannot create a EncKrbCredPart");80} else {81ticketInfo[i] = (KrbCredInfo) new_ticketInfo[i].clone();82}83}84}85timeStamp = new_timeStamp;86usec = new_usec;87nonce = new_nonce;88sAddress = new_sAddress;89rAddress = new_rAddress;90}9192public EncKrbCredPart(byte[] data) throws Asn1Exception,93IOException, RealmException {94init(new DerValue(data));95}9697public EncKrbCredPart(DerValue encoding) throws Asn1Exception,98IOException, RealmException {99init(encoding);100}101102/**103* Initializes an EncKrbCredPart object.104* @param encoding a single DER-encoded value.105* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.106* @exception IOException if an I/O error occurs while reading encoded data.107* @exception RealmException if an error occurs while parsing a Realm object.108*/109private void init(DerValue encoding) throws Asn1Exception,110IOException, RealmException {111DerValue der, subDer;112//may not be the correct error code for a tag113//mismatch on an encrypted structure114nonce = null;115timeStamp = null;116usec = null;117sAddress = null;118rAddress = null;119if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x1D)120|| (encoding.isApplication() != true)121|| (encoding.isConstructed() != true)) {122throw new Asn1Exception(Krb5.ASN1_BAD_ID);123}124der = encoding.getData().getDerValue();125if (der.getTag() != DerValue.tag_Sequence) {126throw new Asn1Exception(Krb5.ASN1_BAD_ID);127}128129subDer = der.getData().getDerValue();130if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x00) {131DerValue[] derValues = subDer.getData().getSequence(1);132ticketInfo = new KrbCredInfo[derValues.length];133for (int i = 0; i < derValues.length; i++) {134ticketInfo[i] = new KrbCredInfo(derValues[i]);135}136} else {137throw new Asn1Exception(Krb5.ASN1_BAD_ID);138}139if (der.getData().available() > 0) {140if (((byte) (der.getData().peekByte()) & (byte) 0x1F) == (byte) 0x01) {141subDer = der.getData().getDerValue();142nonce = subDer.getData().getBigInteger().intValue();143}144}145if (der.getData().available() > 0) {146timeStamp = KerberosTime.parse(der.getData(), (byte) 0x02, true);147}148if (der.getData().available() > 0) {149if (((byte) (der.getData().peekByte()) & (byte) 0x1F) == (byte) 0x03) {150subDer = der.getData().getDerValue();151usec = subDer.getData().getBigInteger().intValue();152}153}154if (der.getData().available() > 0) {155sAddress = HostAddress.parse(der.getData(), (byte) 0x04, true);156}157if (der.getData().available() > 0) {158rAddress = HostAddresses.parse(der.getData(), (byte) 0x05, true);159}160if (der.getData().available() > 0) {161throw new Asn1Exception(Krb5.ASN1_BAD_ID);162}163}164165/**166* Encodes an EncKrbCredPart object.167* @return byte array of encoded EncKrbCredPart object.168* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.169* @exception IOException if an I/O error occurs while reading encoded data.170*171*/172public byte[] asn1Encode() throws Asn1Exception, IOException {173DerOutputStream bytes = new DerOutputStream();174DerOutputStream temp = new DerOutputStream();175DerValue[] tickets = new DerValue[ticketInfo.length];176for (int i = 0; i < ticketInfo.length; i++) {177tickets[i] = new DerValue(ticketInfo[i].asn1Encode());178}179temp.putSequence(tickets);180bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,181true, (byte) 0x00), temp);182183if (nonce != null) {184temp = new DerOutputStream();185temp.putInteger(BigInteger.valueOf(nonce.intValue()));186bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,187true, (byte) 0x01), temp);188}189if (timeStamp != null) {190bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,191true, (byte) 0x02), timeStamp.asn1Encode());192}193if (usec != null) {194temp = new DerOutputStream();195temp.putInteger(BigInteger.valueOf(usec.intValue()));196bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,197true, (byte) 0x03), temp);198}199if (sAddress != null) {200bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,201true, (byte) 0x04), sAddress.asn1Encode());202}203if (rAddress != null) {204bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,205true, (byte) 0x05), rAddress.asn1Encode());206}207temp = new DerOutputStream();208temp.write(DerValue.tag_Sequence, bytes);209bytes = new DerOutputStream();210bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION,211true, (byte) 0x1D), temp);212return bytes.toByteArray();213}214}215216217