// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import krb5.lib.asn1.*;
import java.util.Vector;

/**
 * KDC request
 */
public class KDCReq {

	/**
	 * Version
	 */
	public int pvno;

	/**
	 * Message type
	 */
	public int msgType;

	/**
	 * PA data
	 */
	public PAData[] pAData; //optional

	/**
	 * Request body
	 */
	public KDCReqBody reqBody;

	/**
	 * Class constructor
	 *
	 * @param new_pAData is of type PAData[]
	 * @param new_reqBody is of type KDCReqBody
	 * @param req_type is of type int
	 * @see krb5.lib.PAData
	 * @see krb5.lib.KDCReqBody
	 */
	public KDCReq(PAData[] new_pAData, KDCReqBody new_reqBody,
		int req_type) {
		pvno = Krb5.PVNO;
		msgType = req_type;
		pAData = new_pAData;
		reqBody = new_reqBody;
	}

	/**
	 * Class constructor
	 */
	public KDCReq() {
	}

	/**
	 * Class constructor
	 *
	 * @param data is of type byte[]
	 * @param req_type is of type int
	 * @exception Asn1Exception an exception
	 * @exception BitStringException an exception
	 * @exception RealmException an exception
	 * @exception KrbApErrException an exception
	 * @see krb5.lib.Asn1Exception
	 * @see krb5.lib.BitStringException
	 * @see krb5.lib.RealmException
	 * @see krb5.lib.KrbApErrException
	 */
	public KDCReq(byte[] data, int req_type) throws Asn1Exception,
		BitStringException, RealmException, KrbApErrException {
		this(new EncodeRef(data), req_type);
	}

	/**
	 * Class constructor
	 *
	 * @param ref is of type EncodeRef
	 * @param req_type is of type int
	 * @exception Asn1Exception an exception
	 * @exception BitStringException an exception
	 * @exception RealmException an exception
	 * @exception KrbApErrException an exception
	 * @see krb5.lib.asn1.EncodeRef
	 * @see krb5.lib.Asn1Exception
	 * @see krb5.lib.BitStringException
	 * @see krb5.lib.RealmException
	 * @see krb5.lib.KrbApErrException
	 */
	public KDCReq(EncodeRef ref, int req_type) throws Asn1Exception,
		BitStringException, RealmException, KrbApErrException {
		if (decode.TagApp(ref) != req_type)
			//XXX check if ASN1_BAD_ID is correct return code
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		EncodeRef subRef = decode.Sequence(ref.startOfData());
		if (decode.Tag(subRef) == 1) {
			pvno = decode.Integer(subRef.startOfData());
			if (pvno != Krb5.PVNO)
				//XXX check if KRB_AP_ERR_BADVERSION is correct return code
				throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (decode.Tag(subRef) == 2) {
			msgType = decode.Integer(subRef.startOfData());
			if (msgType != req_type)
				//XXX check if KRB_AP_ERR_MSG_TYPE is correct return code
				throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (decode.Tag(subRef) == 3) {
			Vector tempPAData = new Vector();
			EncodeRef subSubRef = decode.Sequence(subRef.startOfData());
			while (subSubRef.isMoreInSequence()) {
				tempPAData.addElement(new PAData(subSubRef));
				subSubRef.next();
			}
			if (tempPAData.size() > 0) {
				pAData = new PAData[tempPAData.size()];
				tempPAData.copyInto(pAData);
			}
			subRef.next();
		}
		if (decode.Tag(subRef) == 4) {
			reqBody = new KDCReqBody(subRef.startOfData(), msgType);
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (subRef.isMoreInSequence())
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
	}

	/**
	 * Encodes the object in asn1
	 *
	 * @return byte[] is a result
	 * @exception Asn1Exception an exception
	 * @exception BitStringException an exception
	 * @see krb5.lib.Asn1Exception
	 * @see krb5.lib.BitStringException
	 */
	public byte[] asn1Encode() throws Asn1Exception,
		BitStringException {
		if (pAData != null && pAData.length > 0) {
			byte[][] pa_data = new byte[pAData.length][];
			for (int i = 0; i < pAData.length; i++) {
				pa_data[i] = pAData[i].asn1Encode();
			}
			byte[][] kdc_req = {
				encode.prependExplicitTag(1, encode.Integer(pvno)),
				encode.prependExplicitTag(2, encode.Integer(msgType)),
				encode.prependExplicitTag(3, encode.Sequence(pa_data)),
				encode.prependExplicitTag(4, reqBody.asn1Encode(msgType))
			};
			return encode.prependExplicitTag(asn1Class.APPLICATION,
				msgType, encode.Sequence(kdc_req));
		}
		else {
			byte[][] kdc_req = {
				encode.prependExplicitTag(1, encode.Integer(pvno)),
				encode.prependExplicitTag(2, encode.Integer(msgType)),
				encode.prependExplicitTag(4, reqBody.asn1Encode(msgType))
			};
			return encode.prependExplicitTag(asn1Class.APPLICATION,
				msgType, encode.Sequence(kdc_req));
		}
	}

	/**
	 * Encodes the request body in asn1
	 *
	 * @return byte[] is a result
	 * @exception Asn1Exception an exception
	 * @exception BitStringException an exception
	 * @see krb5.lib.Asn1Exception
	 * @see krb5.lib.BitStringException
	 */
	public byte[] asn1EncodeReqBody() throws Asn1Exception,
		BitStringException {
		return reqBody.asn1Encode(msgType);
	}

}