// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import krb5.lib.asn1.*;

/**
 * KRB private
 */
public class KRBPriv {

	/**
	 * Version
	 */
	public int pvno;

	/**
	 * Message type
	 */
	public int msgType;

	/**
	 * Encrypted part
	 */
	public EncryptedData encPart;

	/**
	 * Class constructor
	 *
	 * @param new_encPart is of type EncryptedData
	 * @see krb5.lib.EncryptedData
	 */
	public KRBPriv(EncryptedData new_encPart) {
		pvno = Krb5.PVNO;
		msgType = Krb5.KRB_PRIV;
		encPart = new_encPart;
	}

	/**
	 * Class constructor
	 *
	 * @param data is of type byte[]
	 * @exception Asn1Exception an exception
	 * @exception RealmException an exception
	 * @exception KrbApErrException an exception
	 * @see krb5.lib.Asn1Exception
	 * @see krb5.lib.RealmException
	 * @see krb5.lib.KrbApErrException
	 */
	public KRBPriv(byte[] data) throws Asn1Exception,
		RealmException, KrbApErrException {
		this(new EncodeRef(data));
	}

	/**
	 * Class constructor
	 *
	 * @param ref is of type EncodeRef
	 * @exception Asn1Exception an exception
	 * @exception RealmException an exception
	 * @exception KrbApErrException an exception
	 * @see krb5.lib.asn1.EncodeRef
	 * @see krb5.lib.Asn1Exception
	 * @see krb5.lib.RealmException
	 * @see krb5.lib.KrbApErrException
	 */
	public KRBPriv(EncodeRef ref) throws Asn1Exception,
		RealmException, KrbApErrException {
		if (decode.TagApp(ref) != Krb5.KRB_PRIV)
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		EncodeRef subRef = decode.Sequence(ref.startOfData());

		if (decode.Tag(subRef) == 0) {
			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) == 1) {
			msgType = decode.Integer(subRef.startOfData());
			if (msgType != Krb5.KRB_PRIV)
				//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) == 2) {
			encPart = new EncryptedData(subRef.startOfData());
			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
	 * @see krb5.lib.Asn1Exception
	 */
	public byte[] asn1Encode() throws Asn1Exception {
		byte[][] krb_priv = {
			encode.prependExplicitTag(0, encode.Integer(pvno)),
			encode.prependExplicitTag(1, encode.Integer(msgType)),
			encode.prependExplicitTag(2, encPart.asn1Encode())
		};
		return encode.prependExplicitTag(asn1Class.APPLICATION,
			msgType, encode.Sequence(krb_priv));
	}

}