Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KRBCred.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.EncryptedData;33import sun.security.krb5.Asn1Exception;34import sun.security.krb5.RealmException;35import sun.security.util.*;36import java.util.Vector;37import java.io.IOException;38import java.math.BigInteger;3940/**41* Implements the ASN.1 Authenticator type.42*43* <pre>{@code44* KRB-CRED ::= [APPLICATION 22] SEQUENCE {45* pvno [0] INTEGER (5),46* msg-type [1] INTEGER (22),47* tickets [2] SEQUENCE OF Ticket,48* enc-part [3] EncryptedData -- EncKrbCredPart49* }50* }</pre>51*52* <p>53* This definition reflects the Network Working Group RFC 412054* specification available at55* <a href="http://www.ietf.org/rfc/rfc4120.txt">56* http://www.ietf.org/rfc/rfc4120.txt</a>.57*/58public class KRBCred {5960public Ticket[] tickets = null;61public EncryptedData encPart;62private int pvno;63private int msgType;6465public KRBCred(Ticket[] new_tickets, EncryptedData new_encPart) throws IOException {66pvno = Krb5.PVNO;67msgType = Krb5.KRB_CRED;68if (new_tickets != null) {69tickets = new Ticket[new_tickets.length];70for (int i = 0; i < new_tickets.length; i++) {71if (new_tickets[i] == null) {72throw new IOException("Cannot create a KRBCred");73} else {74tickets[i] = (Ticket) new_tickets[i].clone();75}76}77}78encPart = new_encPart;79}8081public KRBCred(byte[] data) throws Asn1Exception,82RealmException, KrbApErrException, IOException {83init(new DerValue(data));84}8586public KRBCred(DerValue encoding) throws Asn1Exception,87RealmException, KrbApErrException, IOException {88init(encoding);89}9091/**92* Initializes an KRBCred object.93* @param encoding a single DER-encoded value.94* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.95* @exception IOException if an I/O error occurs while reading encoded data.96* @exception KrbApErrException if the value read from the DER-encoded data97* stream does not match the pre-defined value.98* @exception RealmException if an error occurs while parsing a Realm object.99*/100private void init(DerValue encoding) throws Asn1Exception,101RealmException, KrbApErrException, IOException {102if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x16)103|| (encoding.isApplication() != true)104|| (encoding.isConstructed() != true)) {105throw new Asn1Exception(Krb5.ASN1_BAD_ID);106}107DerValue der, subDer;108der = encoding.getData().getDerValue();109if (der.getTag() != DerValue.tag_Sequence) {110throw new Asn1Exception(Krb5.ASN1_BAD_ID);111}112subDer = der.getData().getDerValue();113if ((subDer.getTag() & 0x1F) == 0x00) {114pvno = subDer.getData().getBigInteger().intValue();115if (pvno != Krb5.PVNO) {116throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);117}118} else {119throw new Asn1Exception(Krb5.ASN1_BAD_ID);120}121subDer = der.getData().getDerValue();122if ((subDer.getTag() & 0x1F) == 0x01) {123msgType = subDer.getData().getBigInteger().intValue();124if (msgType != Krb5.KRB_CRED) {125throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);126}127} else {128throw new Asn1Exception(Krb5.ASN1_BAD_ID);129}130subDer = der.getData().getDerValue();131if ((subDer.getTag() & 0x1F) == 0x02) {132DerValue subsubDer = subDer.getData().getDerValue();133if (subsubDer.getTag() != DerValue.tag_SequenceOf) {134throw new Asn1Exception(Krb5.ASN1_BAD_ID);135}136Vector<Ticket> v = new Vector<>();137while (subsubDer.getData().available() > 0) {138v.addElement(new Ticket(subsubDer.getData().getDerValue()));139}140if (v.size() > 0) {141tickets = new Ticket[v.size()];142v.copyInto(tickets);143}144} else {145throw new Asn1Exception(Krb5.ASN1_BAD_ID);146}147encPart = EncryptedData.parse(der.getData(), (byte) 0x03, false);148149if (der.getData().available() > 0) {150throw new Asn1Exception(Krb5.ASN1_BAD_ID);151}152}153154/**155* Encodes an KRBCred object.156* @return the data of encoded EncAPRepPart object.157* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.158* @exception IOException if an I/O error occurs while reading encoded data.159*/160public byte[] asn1Encode() throws Asn1Exception, IOException {161DerOutputStream temp, bytes, out;162temp = new DerOutputStream();163temp.putInteger(BigInteger.valueOf(pvno));164out = new DerOutputStream();165out.write(DerValue.createTag(DerValue.TAG_CONTEXT,166true, (byte) 0x00), temp);167temp = new DerOutputStream();168temp.putInteger(BigInteger.valueOf(msgType));169out.write(DerValue.createTag(DerValue.TAG_CONTEXT,170true, (byte) 0x01), temp);171temp = new DerOutputStream();172for (int i = 0; i < tickets.length; i++) {173temp.write(tickets[i].asn1Encode());174}175bytes = new DerOutputStream();176bytes.write(DerValue.tag_SequenceOf, temp);177out.write(DerValue.createTag(DerValue.TAG_CONTEXT,178true, (byte) 0x02), bytes);179out.write(DerValue.createTag(DerValue.TAG_CONTEXT,180true, (byte) 0x03), encPart.asn1Encode());181bytes = new DerOutputStream();182bytes.write(DerValue.tag_Sequence, out);183out = new DerOutputStream();184out.write(DerValue.createTag(DerValue.TAG_APPLICATION,185true, (byte) 0x16), bytes);186return out.toByteArray();187}188}189190191