Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/KDCReq.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 java.util.Vector;34import sun.security.util.*;35import java.io.IOException;36import java.math.BigInteger;3738/**39* Implements the ASN.1 KRB_KDC_REQ type.40*41* <pre>{@code42* KDC-REQ ::= SEQUENCE {43* -- NOTE: first tag is [1], not [0]44* pvno [1] INTEGER (5) ,45* msg-type [2] INTEGER (10 -- AS -- | 12 -- TGS --),46* padata [3] SEQUENCE OF PA-DATA OPTIONAL47* -- NOTE: not empty --,48* req-body [4] KDC-REQ-BODY49* }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 KDCReq {5960public KDCReqBody reqBody;61public PAData[] pAData = null; //optional62private int pvno;63private int msgType;6465public KDCReq(PAData[] new_pAData, KDCReqBody new_reqBody,66int req_type) throws IOException {67pvno = Krb5.PVNO;68msgType = req_type;69if (new_pAData != null) {70pAData = new PAData[new_pAData.length];71for (int i = 0; i < new_pAData.length; i++) {72if (new_pAData[i] == null) {73throw new IOException("Cannot create a KDCRep");74} else {75pAData[i] = (PAData) new_pAData[i].clone();76}77}78}79reqBody = new_reqBody;80}8182public KDCReq() {83}8485public KDCReq(byte[] data, int req_type) throws Asn1Exception,86IOException, KrbException {87init(new DerValue(data), req_type);88}8990/**91* Creates an KDCReq object from a DerValue object and asn1 type.92*93* @param der a DER value of an KDCReq object.94* @param req_type a encoded asn1 type value.95* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.96* @exception IOException if an I/O error occurs while reading encoded data.97* @exception KrbErrException98*/99public KDCReq(DerValue der, int req_type) throws Asn1Exception,100IOException, KrbException {101init(der, req_type);102}103104/**105* Initializes a KDCReq object from a DerValue. The DER encoding106* must be in the format specified by the KRB_KDC_REQ ASN.1 notation.107*108* @param encoding a DER-encoded KDCReq object.109* @param req_type an int indicating whether it's KRB_AS_REQ or KRB_TGS_REQ type110* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.111* @exception IOException if an I/O error occurs while reading encoded data.112* @exception KrbException if an error occurs while constructing a Realm object,113* or a Krb object from DER-encoded data.114*/115protected void init(DerValue encoding, int req_type) throws Asn1Exception,116IOException, KrbException {117DerValue der, subDer;118BigInteger bint;119if ((encoding.getTag() & 0x1F) != req_type) {120throw new Asn1Exception(Krb5.ASN1_BAD_ID);121}122der = encoding.getData().getDerValue();123if (der.getTag() != DerValue.tag_Sequence) {124throw new Asn1Exception(Krb5.ASN1_BAD_ID);125}126subDer = der.getData().getDerValue();127if ((subDer.getTag() & 0x01F) == 0x01) {128bint = subDer.getData().getBigInteger();129this.pvno = bint.intValue();130if (this.pvno != Krb5.PVNO) {131throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);132}133} else {134throw new Asn1Exception(Krb5.ASN1_BAD_ID);135}136subDer = der.getData().getDerValue();137if ((subDer.getTag() & 0x01F) == 0x02) {138bint = subDer.getData().getBigInteger();139this.msgType = bint.intValue();140if (this.msgType != req_type) {141throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);142}143} else {144throw new Asn1Exception(Krb5.ASN1_BAD_ID);145}146pAData = PAData.parseSequence(der.getData(), (byte) 0x03, true);147subDer = der.getData().getDerValue();148if ((subDer.getTag() & 0x01F) == 0x04) {149DerValue subsubDer = subDer.getData().getDerValue();150reqBody = new KDCReqBody(subsubDer, msgType);151} else {152throw new Asn1Exception(Krb5.ASN1_BAD_ID);153}154}155156/**157* Encodes this object to a byte array.158*159* @return an byte array of encoded data.160* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.161* @exception IOException if an I/O error occurs while reading encoded data.162*163*/164public byte[] asn1Encode() throws Asn1Exception, IOException {165DerOutputStream temp, bytes, out;166temp = new DerOutputStream();167temp.putInteger(BigInteger.valueOf(pvno));168out = new DerOutputStream();169out.write(DerValue.createTag(DerValue.TAG_CONTEXT,170true, (byte) 0x01), temp);171temp = new DerOutputStream();172temp.putInteger(BigInteger.valueOf(msgType));173out.write(DerValue.createTag(DerValue.TAG_CONTEXT,174true, (byte) 0x02), temp);175if (pAData != null && pAData.length > 0) {176temp = new DerOutputStream();177for (int i = 0; i < pAData.length; i++) {178temp.write(pAData[i].asn1Encode());179}180bytes = new DerOutputStream();181bytes.write(DerValue.tag_SequenceOf, temp);182out.write(DerValue.createTag(DerValue.TAG_CONTEXT,183true, (byte) 0x03), bytes);184}185out.write(DerValue.createTag(DerValue.TAG_CONTEXT,186true, (byte) 0x04), reqBody.asn1Encode(msgType));187bytes = new DerOutputStream();188bytes.write(DerValue.tag_Sequence, out);189out = new DerOutputStream();190out.write(DerValue.createTag(DerValue.TAG_APPLICATION,191true, (byte) msgType), bytes);192return out.toByteArray();193}194195public byte[] asn1EncodeReqBody() throws Asn1Exception, IOException {196return reqBody.asn1Encode(msgType);197}198}199200201