// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb4.lib;

import java.io.*;
import krb4.lib.crypto.des;

public class Krb4APRep extends Krb4KRBPriv {
	int checksum;

	public Krb4APRep(
		byte[] key,
		byte[] new_data,
		byte[] new_saddr,
		int sport,
		byte[] raddr,
		int rport
	) throws Krb4Exception, IOException {
		super(
			key,
			new_data,
			new_saddr,
			sport,
			raddr,
			rport
		);
	}

	public Krb4APRep(
		byte[] key,
		byte[] new_data,
		long new_timestamp_5ms,
		byte[] new_saddr,
		int sport,
		byte[] raddr,
		int rport,
		long new_timestamp
	) throws Krb4Exception, IOException {
		super(
			key,
			new_data,
			new_timestamp_5ms,
			new_saddr,
			sport,
			raddr,
			rport,
			new_timestamp
		);
	}

	public Krb4APRep(
		Krb4APReq ap_req,
		long new_timestamp_5ms,
		byte[] new_saddr,
		int sport,
		byte[] raddr,
		int rport,
		long new_timestamp
	) throws Krb4Exception, IOException {
		super(
			ap_req.ticket.sessionKey,
			Krb4Encode.toBytes(ap_req.authenticator.checksum + 1),
			new_timestamp_5ms,
			new_saddr,
			sport,
			raddr,
			rport,
			new_timestamp
		);
	}

	public Krb4APRep(
		Krb4APReq ap_req,
		byte[] new_saddr,
		int sport,
		byte[] raddr,
		int rport
	) throws Krb4Exception, IOException {
		super(
			ap_req.ticket.sessionKey,
			Krb4Encode.toBytes(ap_req.authenticator.checksum + 1),
			new_saddr,
			sport,
			raddr,
			rport
		);
	}

	public Krb4APRep(byte[] data) throws Krb4Exception {
		super(data);
	}

	public void authenticate(byte[] key, int new_checksum)
		throws Krb4Exception, IOException {
		byte[] data = decrypt(key);
		Krb4Encode ref = new Krb4Encode(data);
		ref.setByteOrder(littleEndian);
		checksum = ref.getInt();
		if (checksum != new_checksum + 1)
			//XXX may not be right
			throw new Krb4Exception(Krb4.RD_AP_MODIFIED);
	}

	public void authenticate(Krb4Creds creds, Krb4APReq ap_req)
		throws Krb4Exception, IOException {
		authenticate(creds.sessionKey, ap_req.checksum);
	}		 
}
