// Copyright 1997 The Open Group Research Institute.  All rights reserved.

// test of asn1 classes

import krb5.lib.*;
import krb5.lib.asn1.*;

import java.net.InetAddress;
import java.net.UnknownHostException;

// sample ASN.1 encodings and test driver for the package
public class asn1test {

	public static byte[] sample_as_req() throws Asn1Exception,
		UnknownHostException {

		byte[] kdc_options = { 0, 0, 0, 0 }; //no flags set

		byte[][] name_string = {
			encode.GeneralString("sanfilip")
		};

		byte[][] cname = {
			encode.prependExplicitTag(0, encode.Integer(1)), //name type -- NT_PRINCIPAL
			encode.prependExplicitTag(1, encode.Sequence(name_string)) //name components
		};

		byte[][] name_string2 = {
			encode.GeneralString("krbtgt"),
			encode.GeneralString("booboo.osf.org")
		};

		byte[][] sname = {
			encode.prependExplicitTag(0, encode.Integer(0)), //name type -- NT_DEFAULT
			encode.prependExplicitTag(1, encode.Sequence(name_string2)) //name components
		};

		byte[][] etype = {
			encode.Integer(3), //encryption type -- des-cbc-md5
			encode.Integer(1), //encryption type -- des-cbc-crc
		};

		byte[] octet_string = InetAddress.getLocalHost().getAddress(); //ip address

		byte[][] host_address = {
			encode.prependExplicitTag(0, encode.Integer(2)), //host address type -- Internet
			encode.prependExplicitTag(1, encode.OctetString(octet_string)) //host address data
		};

		byte[][] host_addresses = {
			encode.Sequence(host_address) //host address
		};

		byte[][] kdc_req_body = {
			encode.prependExplicitTag(0, encode.BitString(kdc_options)), //options -- none
			encode.prependExplicitTag(1, encode.Sequence(cname)), //client principal
			encode.prependExplicitTag(2, encode.GeneralString("booboo.osf.org")), //realm
			encode.prependExplicitTag(3, encode.Sequence(sname)), //server principal
			encode.prependExplicitTag(5, encode.GeneralizedTime("19700101000000Z")), //exp time
			encode.prependExplicitTag(7, encode.Integer((int)(System.currentTimeMillis() / 1000))),  //nonce from current time
			encode.prependExplicitTag(8, encode.Sequence(etype)), //encryption types
			encode.prependExplicitTag(9, encode.Sequence(host_addresses)) //host addresses
		};

		byte[][] kdc_req = {
			encode.prependExplicitTag(1, encode.Integer(5)), //version
			encode.prependExplicitTag(2, encode.Integer(10)), //msg-type
			encode.prependExplicitTag(4, encode.Sequence(kdc_req_body))
		};

		return encode.prependExplicitTag(asn1Class.APPLICATION, 10,
			encode.Sequence(kdc_req));
	}

	public static final byte[] sample_as_rep = {
		(byte)0x6b, (byte)0x82, (byte)0x02, (byte)0x20, (byte)0x30, (byte)0x82, (byte)0x02, (byte)0x1c, (byte)0xa0, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x05, (byte)0xa1, (byte)0x03, (byte)0x02,
		(byte)0x01, (byte)0x0b, (byte)0xa3, (byte)0x10, (byte)0x1b, (byte)0x0e, (byte)0x62, (byte)0x6f, (byte)0x6f, (byte)0x62, (byte)0x6f, (byte)0x6f, (byte)0x2e, (byte)0x6f, (byte)0x73, (byte)0x66,
		(byte)0x2e, (byte)0x6f, (byte)0x72, (byte)0x67, (byte)0xa4, (byte)0x15, (byte)0x30, (byte)0x13, (byte)0xa0, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x01, (byte)0xa1, (byte)0x0c, (byte)0x30,
		(byte)0x0a, (byte)0x1b, (byte)0x08, (byte)0x73, (byte)0x61, (byte)0x6e, (byte)0x66, (byte)0x69, (byte)0x6c, (byte)0x69, (byte)0x70, (byte)0xa5, (byte)0x81, (byte)0xf8, (byte)0x61, (byte)0x81,
		(byte)0xf5, (byte)0x30, (byte)0x81, (byte)0xf2, (byte)0xa0, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x05, (byte)0xa1, (byte)0x10, (byte)0x1b, (byte)0x0e, (byte)0x62, (byte)0x6f, (byte)0x6f,
		(byte)0x62, (byte)0x6f, (byte)0x6f, (byte)0x2e, (byte)0x6f, (byte)0x73, (byte)0x66, (byte)0x2e, (byte)0x6f, (byte)0x72, (byte)0x67, (byte)0xa2, (byte)0x23, (byte)0x30, (byte)0x21, (byte)0xa0,
		(byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xa1, (byte)0x1a, (byte)0x30, (byte)0x18, (byte)0x1b, (byte)0x06, (byte)0x6b, (byte)0x72, (byte)0x62, (byte)0x74, (byte)0x67, (byte)0x74,
		(byte)0x1b, (byte)0x0e, (byte)0x62, (byte)0x6f, (byte)0x6f, (byte)0x62, (byte)0x6f, (byte)0x6f, (byte)0x2e, (byte)0x6f, (byte)0x73, (byte)0x66, (byte)0x2e, (byte)0x6f, (byte)0x72, (byte)0x67,
		(byte)0xa3, (byte)0x81, (byte)0xb3, (byte)0x30, (byte)0x81, (byte)0xb0, (byte)0xa0, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x01, (byte)0xa1, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x01,
		(byte)0xa2, (byte)0x81, (byte)0xa3, (byte)0x04, (byte)0x81, (byte)0xa0, (byte)0x96, (byte)0x7e, (byte)0x72, (byte)0x48, (byte)0x39, (byte)0x81, (byte)0x7e, (byte)0x22, (byte)0x9e, (byte)0x40,
		(byte)0x2e, (byte)0xc1, (byte)0x44, (byte)0xf0, (byte)0x71, (byte)0x9a, (byte)0xab, (byte)0xb1, (byte)0x3c, (byte)0xc0, (byte)0x09, (byte)0x18, (byte)0x59, (byte)0xdd, (byte)0x46, (byte)0x36,
		(byte)0x67, (byte)0x0e, (byte)0x85, (byte)0xe4, (byte)0x5c, (byte)0xec, (byte)0x35, (byte)0x7f, (byte)0x25, (byte)0xff, (byte)0x97, (byte)0xfc, (byte)0xe7, (byte)0x61, (byte)0xcd, (byte)0x05,
		(byte)0xd4, (byte)0x1c, (byte)0x1e, (byte)0xa5, (byte)0x8b, (byte)0x67, (byte)0x16, (byte)0x10, (byte)0xcc, (byte)0x35, (byte)0xe5, (byte)0x3b, (byte)0xa8, (byte)0x71, (byte)0xc5, (byte)0xbf,
		(byte)0xfe, (byte)0xb4, (byte)0xc3, (byte)0x70, (byte)0x29, (byte)0x9a, (byte)0xde, (byte)0x13, (byte)0xe3, (byte)0x8b, (byte)0x2e, (byte)0xb1, (byte)0x45, (byte)0x1e, (byte)0x4e, (byte)0x7c,
		(byte)0x10, (byte)0xa1, (byte)0xb8, (byte)0x66, (byte)0xdd, (byte)0x5b, (byte)0xa7, (byte)0x23, (byte)0x11, (byte)0x54, (byte)0x0c, (byte)0x6e, (byte)0x54, (byte)0xba, (byte)0x51, (byte)0x6d,
		(byte)0xf6, (byte)0xb4, (byte)0x43, (byte)0xe0, (byte)0xb9, (byte)0xfc, (byte)0xac, (byte)0x21, (byte)0x38, (byte)0xa4, (byte)0xdd, (byte)0x58, (byte)0x7f, (byte)0xb5, (byte)0xad, (byte)0x13,
		(byte)0x11, (byte)0x8d, (byte)0xb2, (byte)0x22, (byte)0x82, (byte)0x1f, (byte)0xc3, (byte)0x83, (byte)0x73, (byte)0x47, (byte)0xd0, (byte)0x3b, (byte)0xeb, (byte)0xea, (byte)0xdf, (byte)0xd5,
		(byte)0x1d, (byte)0x57, (byte)0xc3, (byte)0x06, (byte)0x63, (byte)0xa6, (byte)0x94, (byte)0xb9, (byte)0x67, (byte)0xbb, (byte)0x66, (byte)0xfc, (byte)0x33, (byte)0xa0, (byte)0x1d, (byte)0x13,
		(byte)0xf8, (byte)0xa2, (byte)0xdc, (byte)0xec, (byte)0x43, (byte)0x23, (byte)0x25, (byte)0x3a, (byte)0x42, (byte)0x4c, (byte)0x36, (byte)0x81, (byte)0x11, (byte)0x73, (byte)0x66, (byte)0x49,
		(byte)0xdb, (byte)0x61, (byte)0x56, (byte)0xc3, (byte)0xc6, (byte)0x7f, (byte)0xa6, (byte)0x81, (byte)0xeb, (byte)0x30, (byte)0x81, (byte)0xe8, (byte)0xa0, (byte)0x03, (byte)0x02, (byte)0x01,
		(byte)0x03, (byte)0xa1, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x01, (byte)0xa2, (byte)0x81, (byte)0xdb, (byte)0x04, (byte)0x81, (byte)0xd8, (byte)0xda, (byte)0xef, (byte)0x70, (byte)0x14,
		(byte)0xe1, (byte)0x32, (byte)0xa5, (byte)0x61, (byte)0x46, (byte)0xe4, (byte)0x45, (byte)0xb3, (byte)0x84, (byte)0x78, (byte)0xa1, (byte)0xb5, (byte)0x32, (byte)0x20, (byte)0xb0, (byte)0xe0,
		(byte)0x06, (byte)0xba, (byte)0x84, (byte)0x10, (byte)0x27, (byte)0xb4, (byte)0xc3, (byte)0x83, (byte)0x45, (byte)0x5f, (byte)0x20, (byte)0xdc, (byte)0x19, (byte)0x13, (byte)0x1a, (byte)0x7b,
		(byte)0xa5, (byte)0xb7, (byte)0xd2, (byte)0x8a, (byte)0x37, (byte)0x40, (byte)0x53, (byte)0xe0, (byte)0x51, (byte)0x57, (byte)0x22, (byte)0x9e, (byte)0x70, (byte)0xe4, (byte)0x90, (byte)0xe7,
		(byte)0xbd, (byte)0x1b, (byte)0x29, (byte)0x49, (byte)0x21, (byte)0xe6, (byte)0x66, (byte)0xdb, (byte)0x8b, (byte)0x4a, (byte)0x7d, (byte)0x1a, (byte)0x3f, (byte)0xaa, (byte)0x7f, (byte)0x98,
		(byte)0x20, (byte)0xa1, (byte)0x29, (byte)0x13, (byte)0x79, (byte)0x62, (byte)0x4a, (byte)0xbe, (byte)0x0a, (byte)0xa5, (byte)0x12, (byte)0x2d, (byte)0x9f, (byte)0x91, (byte)0x6a, (byte)0x04,
		(byte)0xd4, (byte)0xdb, (byte)0x3e, (byte)0x7c, (byte)0x81, (byte)0x5a, (byte)0x25, (byte)0x26, (byte)0x7e, (byte)0x05, (byte)0xe2, (byte)0x2f, (byte)0x78, (byte)0x36, (byte)0x1b, (byte)0x3b,
		(byte)0xb1, (byte)0xaf, (byte)0xd5, (byte)0x3c, (byte)0xb2, (byte)0xc8, (byte)0xff, (byte)0x38, (byte)0x44, (byte)0xe9, (byte)0x66, (byte)0xe0, (byte)0x26, (byte)0xf8, (byte)0xcd, (byte)0x06,
		(byte)0xe0, (byte)0xe6, (byte)0x5e, (byte)0xb9, (byte)0x3e, (byte)0x8b, (byte)0x28, (byte)0xcc, (byte)0x85, (byte)0xe5, (byte)0xe1, (byte)0xcd, (byte)0x69, (byte)0xc6, (byte)0x3b, (byte)0x35,
		(byte)0xca, (byte)0x33, (byte)0x7e, (byte)0x1c, (byte)0xb5, (byte)0x6d, (byte)0x44, (byte)0xb1, (byte)0x95, (byte)0x1b, (byte)0x9c, (byte)0x11, (byte)0x05, (byte)0x03, (byte)0x0b, (byte)0xae,
		(byte)0x72, (byte)0x74, (byte)0x41, (byte)0x06, (byte)0x7e, (byte)0x73, (byte)0xd7, (byte)0xfe, (byte)0x5c, (byte)0xc9, (byte)0x23, (byte)0x19, (byte)0xa5, (byte)0x0a, (byte)0x93, (byte)0xaf,
		(byte)0xf2, (byte)0x03, (byte)0x15, (byte)0xec, (byte)0x53, (byte)0x6a, (byte)0xc6, (byte)0xa0, (byte)0x9d, (byte)0x96, (byte)0xcd, (byte)0xaa, (byte)0xaa, (byte)0x07, (byte)0x0c, (byte)0xf8,
		(byte)0x62, (byte)0x92, (byte)0xe9, (byte)0xf0, (byte)0x29, (byte)0x74, (byte)0x6a, (byte)0xa3, (byte)0x06, (byte)0xe0, (byte)0x42, (byte)0x50, (byte)0x8d, (byte)0x21, (byte)0x51, (byte)0x35,
		(byte)0x88, (byte)0xcd, (byte)0x80, (byte)0xb8, (byte)0x39, (byte)0x8a, (byte)0xfd, (byte)0x94, (byte)0x08, (byte)0x37, (byte)0xab, (byte)0x7f, (byte)0x46, (byte)0x7a, (byte)0xf3, (byte)0xae,
		(byte)0xac, (byte)0xb4, (byte)0x33, (byte)0xbf
	};

	public static void main(String args[])
		throws Asn1Exception, UnknownHostException {

		int x = 3;
		byte[] data = new byte[1];
		data[0] = (byte)x;
		System.out.println(util.byte2hexString(data));
		data = encode.prependLength(data);
		System.out.println(util.byte2hexString(data));
		//data = encode.prependTag(0x7fffffff, data);
		data = encode.prependTag(2, data);
		System.out.println(util.byte2hexString(data));
		encode.setClass(asn1Class.PRIVATE, data);
		System.out.println(util.byte2hexString(data));
		encode.setMethod(asn1Method.CONSTRUCTED, data);
		System.out.println(util.byte2hexString(data));
		System.out.println(util.byte2hexString(encode.Integer(3)));
		System.out.println();

		System.out.println("BIT STRING:");
		byte[] bit_string = { (byte)0x6e, (byte)0x5d, (byte)0xc0 };
		System.out.println(util.byte2hexString(encode.BitString(
			bit_string, 18)));
		System.out.println(util.byte2hexString(encode.BitString(
			bit_string)));
		System.out.println();

		System.out.println("BOOLEAN:");
		System.out.println(util.byte2hexString(encode.Boolean(true)));
		System.out.println(util.byte2hexString(encode.Boolean(false)));
		System.out.println();

		System.out.println("GeneralString:");
		System.out.println(util.byte2hexString(encode.GeneralString(
			"krbtgt\u0000/{{{\u00ff[[[@\u0001osf.org")));
		System.out.println();

		System.out.println("GeneralizedTime:");
		System.out.println(util.byte2hexString(encode.GeneralizedTime(
			"910506234540Z")));
		System.out.println();

		System.out.println("IA5String:");
		System.out.println(util.byte2hexString(encode.IA5String(
			"test1@rsa.com")));
		System.out.println();

		System.out.println("INTEGER:");
		System.out.println(util.byte2hexString(encode.Integer(0)));
		System.out.println(util.byte2hexString(encode.Integer(127)));
		System.out.println(util.byte2hexString(encode.Integer(128)));
		System.out.println(util.byte2hexString(encode.Integer(256)));
		System.out.println(util.byte2hexString(encode.Integer(-128)));
		System.out.println(util.byte2hexString(encode.Integer(-129)));
		System.out.println();

		System.out.println("NULL:");
		System.out.println(util.byte2hexString(encode.Null()));
		System.out.println();

		System.out.println("NumericString:");
		System.out.println(util.byte2hexString(encode.NumericString(
			"910506 234540")));
		System.out.println();

		System.out.println("OBJECT IDENTIFIER:");
		int[] object_identifier = { 1, 2, 840, 113549 };
		byte[] encOid = encode.ObjectIdentifier(object_identifier);
		System.out.println(util.byte2hexString(encOid));
		System.out.println();

		util.print(encOid);
		System.out.println();

		System.out.println("OCTET STRING:");
		byte[] octet_string = { (byte)0x01, (byte)0x23, (byte)0x45,
			(byte)0x67, (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef };
		System.out.println(util.byte2hexString(encode.OctetString(
			octet_string)));
		System.out.println();

		System.out.println("PrintableString:");
		System.out.println(util.byte2hexString(encode.PrintableString(
			"Test User 1")));
		System.out.println();

		System.out.println("T61String:");
		System.out.println(util.byte2hexString(encode.T61String(
			"cl\u00c2es publiques")));
		System.out.println();

		System.out.println("UTCTime:");
		System.out.println(util.byte2hexString(encode.UTCTime(
			"910506234540Z")));
		System.out.println();

		System.out.println("SEQUENCE:");
		int[] object_identifier2 = { 2, 5, 4, 6 };
		byte[][] sequence = {
			encode.ObjectIdentifier(object_identifier2),
			encode.PrintableString("US")
		};
		System.out.println(util.byte2hexString(encode.Sequence(sequence)));
		System.out.println();

		System.out.println("AS_REQ:");

		byte[] msg = sample_as_req();

		System.out.println(util.byte2hexString(msg));
		System.out.println(msg.length + " bytes");
		System.out.println();

		msg = new byte[1];
		data = encode.prependTag(0x0fffffff, encode.prependLength(msg));
		int tag = decode.Tag(data, 0);

		System.out.println(util.byte2hexString(data));
		System.out.println("tag: " + tag);
		System.out.println();

		System.out.println("INTEGER:");
		byte[] a,b,c,d,e,f;
		a = encode.Integer(0);
		b = encode.Integer(127);
		c = encode.Integer(128);
		d = encode.Integer(256);
		e = encode.Integer(-128);
		f = encode.Integer(-129);
		System.out.println(0 + " : " + util.byte2hexString(a));
		System.out.println(127 + " : " + util.byte2hexString(b));
		System.out.println(128 + " : " + util.byte2hexString(c));
		System.out.println(256 + " : " + util.byte2hexString(d));
		System.out.println(-128 + " : " + util.byte2hexString(e));
		System.out.println(-129 + " : " + util.byte2hexString(f));
		System.out.println();
		System.out.println(0 + " : " + decode.Integer(a, 0));
		System.out.println(127 + " : " + decode.Integer(b, 0));
		System.out.println(128 + " : " + decode.Integer(c, 0));
		System.out.println(256 + " : " + decode.Integer(d, 0));
		System.out.println(-128 + " : " + decode.Integer(e, 0));
		System.out.println(-129 + " : " + decode.Integer(f, 0));
		System.out.println();

		System.out.println("OCTET STRING:");
		byte[] octet_string2 = { (byte)0x01, (byte)0x23, (byte)0x45,
			(byte)0x67, (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef };
		byte[] encoded_octet_string = encode.OctetString(octet_string2);
		System.out.println(util.byte2hexString(encoded_octet_string));
		System.out.println();
		System.out.println(util.byte2hexString(decode.OctetString(encoded_octet_string, 0)));
		System.out.println();

		System.out.println("GeneralString:");
		byte[] encoded_general_string = encode.GeneralString(
			"krbtgt/bambam.osf.org");
		System.out.println(util.byte2hexString(encoded_general_string));
		System.out.println();
		System.out.println(decode.GeneralString(encoded_general_string, 0));
		System.out.println();

		System.out.println("BIT STRING:");
		byte[] bit_string2 = { (byte)0x6e, (byte)0x5d, (byte)0xc0 };
		byte[] encoded_bit_string = encode.BitString(bit_string2, 18);
		byte[] encoded_bit_string2 = encode.BitString(bit_string2); 
		System.out.println(util.byte2hexString(encoded_bit_string));
		System.out.println(util.byte2hexString(encoded_bit_string2));
		System.out.println();
		System.out.println(util.byte2hexString(
			decode.BitString(encoded_bit_string, 0)));
		System.out.println(util.byte2hexString(
			decode.BitString(encoded_bit_string2, 0)));
		System.out.println();

		System.out.println("AS_REQ:");
		util.print(sample_as_req());
		System.out.println();

		System.out.println("AS_REP:");
		util.print(sample_as_rep);

	}

}