#include "test_common.h"
#ifdef WIN32
#include "getopt.h"
#else 
extern char *optarg;
#endif

void usage(void);
CK_RV test_crypto();
int test_tls_prf(CK_SESSION_HANDLE sess);

CK_SLOT_ID SlotId;

CK_SLOT_ID_PTR SlotList = NULL;
CK_ULONG SlotCount = 0;

int main(int argc, char *argv[]) {
   CK_RV rc = 1;
   CK_FLAGS flags = 0;
   unsigned long slot_num = 0;  
   int i;

   // parse the command line parameters
	for (i=1; i<argc; i++) {
		if (strcmp("-api", argv[i]) == 0) {
			i++;
			api_path = argv[i];
		} else if (strcmp("-user_pin", argv[i]) == 0) {
			i++;
			user_pin = argv[i];
		}
	}

   // load the PKCS11 library 
	rc = init(api_path, &funcs, NULL);
   if (rc != CKR_OK) {
      fprintf(stderr, "ERROR calling init, rc = %p.\n", (void *)rc);
      return rc;
   }
	rc = get_slot_list(funcs, 
                    &SlotList, 
                    &SlotCount);
	if (rc != CKR_OK) {
		printf("get_slot_list failed, rc = 0x%x\n", rc );
		return rc;
	}
	rc = find_slot_with_token(funcs, 
                           SlotList, 
                           SlotCount, 
                           &SlotId);
	if (rc != CKR_OK) {
		printf("find_slot_with_token failed, rc = 0x%x\n", rc );
		return rc;
	}
		// test the crypto functions 
//	for (i=0; i<40; i++) { 
		rc = test_crypto();
		if (rc != CKR_OK) {
			fprintf(stderr, "ERROR call to lissi failed.\n");
			return rc;
		}
//	}
	return CKR_OK;
}


void usage(void)
{
   fprintf(stderr, "Usage: lissi [-c <slotId>]\n"
      "Default slotID is 0\n"
      "To get a list of slotIDs, call pkcs11_cfg -s\n");
   exit(-1);
}

CK_RV test_crypto() 
{
   CK_RV rc, ret;
   CK_SESSION_HANDLE hSession;
	CK_MECHANISM_INFO minfo;

   // open a R/W cryptoki session, CKR_SERIAL_SESSION is a legacy bit we have to set
   rc = funcs->C_OpenSession(SlotId, CKF_RW_SESSION | CKF_SERIAL_SESSION, NULL_PTR,
         NULL_PTR, &hSession);
   if (rc != CKR_OK) {
      fprintf(stderr, "ERROR call to C_OpenSession failed, rc = %p\n", (void *)rc);
      goto out;
   }
  fprintf(stderr, "C_OpenSession success\n");
/*
   // log in as normal user 
   rc = funcs->C_Login(hSession, CKU_USER, user_pin, strlen(user_pin));
   if (rc != CKR_OK) {
      fprintf(stderr, "ERROR call to C_Login failed, rc = %p\n", (void *)rc);
      goto out_close;
   }
  fprintf(stderr, "C_Login success\n");
*/
   
	rc = funcs->C_GetMechanismInfo(SlotId, CKM_TLS_GOST_PRF, &minfo);
	if (rc != CKR_OK) {
		fprintf(stderr, "\n======== Mechanism CKM_TLS_GOST_PRF not supported ===========\n");
	} else {
		fprintf(stderr, "\n==================== CKM_TLS_GOST_PRF test ==================\n");
		rc = test_tls_prf(hSession);
		if (rc != CKR_OK) {
			fprintf(stderr, "ERROR CKM_TLS_GOST_PRF failed, rc = %p\n", (void *)rc);
		} else {
			fprintf(stderr, "CKM_TLS_GOST_PRF test passed.\n");
		}
	}
   
//out_close:
   if( (ret = funcs->C_CloseSession(hSession)) != CKR_OK ) {
      fprintf(stderr, "Error: C_CloseSession failed with %p\n", (void *)ret);
	  rc = ret;
   }
   else {
      fprintf(stderr, "C_CloseSession success\n");
   }

out:
   return rc;
}

int test_tls_prf(CK_SESSION_HANDLE sess)
{
	int rc = 0;
	static CK_BYTE value[12];
   	CK_MECHANISM mechanism_desc = {CKM_TLS_GOST_PRF, NULL, 0};
   	CK_MECHANISM_PTR mechanism = &mechanism_desc;
	CK_OBJECT_HANDLE keyh = CK_INVALID_HANDLE;
	static CK_OBJECT_CLASS oclass = CKO_SECRET_KEY;
	static CK_KEY_TYPE key_type = CKK_GENERIC_SECRET;
	static CK_BBOOL ltrue = CK_TRUE;	
	// Using master secret (48 bytes) from LCJSSE test utility.
	static CK_BYTE keyval[] = {
		0x76,0x22,0x41,0x3d,0x5a,0xb6,0x7f,0x5b,
		0x86,0x2e,0x75,0x97,0xe8,0xf9,0x31,0xe4,
		0x2f,0x13,0x13,0xd5,0x61,0xed,0xab,0xed,
		0xa5,0x4b,0xd8,0x5d,0x60,0x0a,0xec,0x99,
		0x45,0xfe,0x39,0xa1,0xb7,0x62,0x0d,0x66,
		0x3c,0xdd,0xb3,0x71,0x90,0x0f,0xc3,0xdd
	};
	// Using real TLS message label from LirJSSE test utility.
	static CK_BYTE label[] = "client finished";	
	// seed (64 bytes) = client_random (32 bytes) + server_random (32 bytes)	
	// Using concatenated client_random and server_random data (32+32 bytes)
	// from LCJSSE test utility.
	static CK_BYTE seed[] = {
		0x87,0x5a,0x38,0x94,0xa4,0xf9,0x3c,0x30,
		0x65,0xfc,0x66,0xbe,0xf2,0xc0,0x09,0xb9,
		0xc3,0x26,0x3e,0x16,0xc7,0x28,0x14,0x27,
		0x98,0x26,0xe4,0xd5,0x30,0x7f,0x9a,0x2d,
		0x87,0x0e,0x1d,0x34,0xaf,0x28,0x1c,0x60,
		0xae,0x8d,0x26,0xe4,0xd5,0x30,0x7f,0x9a,
		0x2d,0x87,0x0e,0x1d,0x34,0xaf,0x28,0x1c,
		0x60,0xae,0x8d,0x21,0xee,0x7d,0x52,0x44
	};
	// Using resulting TLS PRF value (12 bytes) from LCJSSE test utility.	
	static CK_BYTE et_gost[] = {
		0x2a,0x6b,0x4c,0x14,0x61,0xb1,0x39,0x2d,
		0x18,0xdf,0x8a,0x8d
	};
	static CK_ATTRIBUTE key_template[] = {
		{ CKA_VALUE, keyval, sizeof(keyval) },
		{ CKA_CLASS, &oclass, sizeof(oclass) },
		{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
		{ CKA_DERIVE, &ltrue, sizeof(ltrue) }
	};
	static CK_ULONG len = sizeof(value);
	// CryptoPro GOST 3411 HASH 1 Param Set OID
	static CK_BYTE oid_hash_1[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01 
	};
	static CK_TLS_GOST_PRF_PARAMS params_gost = {
	    {
			seed, sizeof(seed),
			label, sizeof(label) - 1,
			value, &len
	    },
	    oid_hash_1,
	    sizeof(oid_hash_1)
	};

	rc = funcs->C_CreateObject(sess, 
		key_template, sizeof(key_template)/sizeof(CK_ATTRIBUTE), 
		&keyh);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_CreateObject failed: 0x%x\n", rc);
		return rc;
	}

	mechanism->pParameter = &params_gost;
	mechanism->ulParameterLen = sizeof(params_gost);
	rc = funcs->C_DeriveKey(sess, mechanism, keyh, NULL_PTR, 0, NULL_PTR);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DeriveKey failed: 0x%x\n", rc);
		return rc;
	}

	CHECK(et_gost);

	rc = funcs->C_DestroyObject(sess, keyh);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DestroyObject failed: 0x%x\n", rc);
		return rc;
	}
	
	return rc;
}



