// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import krb5.lib.asn1.*;

/**
 * Authorization data entry
 */
public class AuthorizationDataEntry implements Cloneable {

	/**
	 * Authorization data type
	 */
	public int adType;

	/**
	 * Authorization data 
	 */
	public byte[] adData;

	/**
	 * Class constructor
	 */
	private AuthorizationDataEntry() {
	}

	/**
	 * Class constructor
	 *
	 * @param new_adType is of type int
	 * @param new_adData is of type byte[]
	 */
	public AuthorizationDataEntry(
		int new_adType,
		byte[] new_adData
	) {
		adType = new_adType;
		adData = new_adData;
	}

	/**
	 * Clones a copy of the object
	 *
	 * @return Object is a result
	 */
	public Object clone() {
		AuthorizationDataEntry new_authorizationDataEntry =
			new AuthorizationDataEntry();
		new_authorizationDataEntry.adType = adType;
		if (adData != null) {
			new_authorizationDataEntry.adData = new byte[adData.length];
			System.arraycopy(adData, 0,
				new_authorizationDataEntry.adData, 0, adData.length);
		}
		return new_authorizationDataEntry;
	}

	/**
	 * Class constructor
	 *
	 * @param ref is of type EncodeRef
	 * @exception Asn1Exception an exception
	 * @see krb5.lib.asn1.EncodeRef
	 * @see krb5.lib.Asn1Exception
	 */
	public AuthorizationDataEntry(EncodeRef ref) throws Asn1Exception {
		EncodeRef subRef = decode.Sequence(ref);
		if (decode.Tag(subRef) == 0) {
			adType = decode.Integer(subRef.startOfData());
			//XXX need to validate that the type is recognized
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (decode.Tag(subRef) == 1) {
			adData = decode.OctetString(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[][] authorization_data_entry = {
			encode.prependExplicitTag(0, encode.Integer(adType)),
			encode.prependExplicitTag(1, encode.OctetString(adData))
		};
		return encode.Sequence(authorization_data_entry);
	}

}