//mskrbhttp.java

import java.io.*;
import java.net.*;
import krb5.lib.*;
import krb5.lib.asn1.*;
import krb5.lib.crypto.*;
import asn1test;

public class mskrbhttp {
	public static void main(String args[]) throws FileNotFoundException,
	IOException, Asn1Exception {
		File f = new File("c:\\temp\\mskrbhttp.bin");
		int file_len = (int)f.length();
		FileInputStream inp = new FileInputStream(f);
		byte[] b = new byte[file_len];
		int index = 0;
		int read_len = 0;
		while (index < file_len && read_len != -1) {
			read_len = inp.available();
			read_len = inp.read(b, index, read_len);
			index += read_len;
		}
		System.out.println(b.length + " bytes:");
		System.out.println(util.byte2hexString(b));
		System.out.println();
		krb5.lib.asn1.util.print(b);
		System.out.println();
	}


	void junk() {
		byte[] obuf = null;
		byte[] obuf2 = null;
		try {
			obuf = libtest.as_req(
				new KDCOptions(),
				new PrincipalName("sanfilip", Krb5.KRB_NT_PRINCIPAL),
				new Realm("cordelia.cell.osf.org")
			);
		} catch (Asn1Exception e) {
			System.out.println(e);
		} catch (BitStringException e) {
			System.out.println(e);
		} catch (KrbApErrException e) {
			System.out.println(e);
		} catch (RealmException e) {
			System.out.println(e);
		} catch (UnknownHostException e) {
			System.out.println(e);
		}
		byte  ibuf[] = new byte[1024];
		byte  ibuf2[] = new byte[1024];
		InetAddress  iaddr = null;
		int iport = 88;
		DatagramSocket dgSocket;
		DatagramSocket dgSocket2;

		try {
			//iaddr = InetAddress.getByName("sanfilippo.osf.org");
			iaddr = InetAddress.getByName("homer.osf.org");
		}
		catch (UnknownHostException e) {
			System.out.println(e);
		}

		DatagramPacket dgPacketOut = new DatagramPacket(obuf, obuf.length,
			iaddr, iport);
		DatagramPacket dgPacketIn = new DatagramPacket(ibuf, ibuf.length);

		try {
			System.out.println("as_req: " + obuf.length + " bytes");
			krb5.lib.asn1.util.print(obuf);
			System.out.println("\npress enter to send as_req");
			System.in.read();
			dgSocket = new DatagramSocket();
			dgSocket.send(dgPacketOut);
			dgSocket.receive(dgPacketIn);
			System.out.println("as_rep: " + dgPacketIn.getLength() +
				" bytes");
			byte[] temp = new byte[dgPacketIn.getLength()];
			System.arraycopy(dgPacketIn.getData(), 0, temp, 0,
				dgPacketIn.getLength()); 
			System.out.println(util.byte2hexString(temp));
			System.out.println();
			krb5.lib.asn1.util.print(temp);
			EncodeRef ref = new EncodeRef(temp);
			ASRep rep = new ASRep(ref);
			byte[] enc_as_rep_part =
				rep.encPart.decrypt("somewha0cordelia.cell.osf.orgsanfilip");
			ref = new EncodeRef(enc_as_rep_part);
			EncASRepPart enc_part = new EncASRepPart(ref);
			krb5.lib.asn1.util.print(enc_part.asn1Encode());
			System.out.println();

			obuf2 = libtest.tgs_req(
				new KDCOptions(),
				rep.ticket,
				enc_part.key,
				new PrincipalName("sanfilip", Krb5.KRB_NT_PRINCIPAL),
				new Realm("cordelia.cell.osf.org"),
				new PrincipalName("WandXXXXOnOutpost", Krb5.KRB_NT_SRV_INST)
			);

			System.out.println("tgs_req: " + obuf2.length + " bytes");
			krb5.lib.asn1.util.print(obuf2);
			System.out.println("\npress enter to send tgs_req");
			System.in.read();
			DatagramPacket dgPacketOut2 = new DatagramPacket(obuf2, obuf2.length,
				iaddr, iport);
			DatagramPacket dgPacketIn2 = new DatagramPacket(ibuf2, ibuf2.length);

			dgSocket2 = new DatagramSocket();
			dgSocket2.send(dgPacketOut2);
			dgSocket2.receive(dgPacketIn2);
			System.out.println("tgs_rep: " + dgPacketIn2.getLength() +
				" bytes");
			byte[] temp2 = new byte[dgPacketIn2.getLength()];
			System.arraycopy(dgPacketIn2.getData(), 0, temp2, 0,
				dgPacketIn2.getLength()); 
			System.out.println(util.byte2hexString(temp2));
			System.out.println();
			krb5.lib.asn1.util.print(temp2);
			EncodeRef ref2 = new EncodeRef(temp2);
			TGSRep rep2 = new TGSRep(ref2);
			byte[] enc_tgs_rep_part =
				rep2.encPart.decrypt(enc_part.key);
			ref2 = new EncodeRef(enc_tgs_rep_part);
			EncTGSRepPart enc_part2 = new EncTGSRepPart(ref2);
			krb5.lib.asn1.util.print(enc_part2.asn1Encode());
			System.out.println();

		
		} catch (Asn1Exception e) {
			System.out.println(e);
		} catch (BitStringException e) {
			System.out.println(e);
		} catch (SocketException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		} catch (KdcErrException e) {
			System.out.println(e);
		} catch (KrbApErrException e) {
			System.out.println(e);
		} catch (RealmException e) {
			System.out.println(e);
		} catch (RuntimeException e) {
			System.out.println(e);
		}
		System.out.println("ok");
	}
}