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

void usage(void);
CK_RV test_crypto();
int test_tls_pre_master_key_gen(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;
	}
//	for (i=0; i<40; i++) {
		// test the crypto functions
		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_PRE_MASTER_KEY_GEN, &minfo);
	if (rc != CKR_OK) {
		fprintf(stderr, "\n======== Mechanism CKM_TLS_GOST_PRE_MASTER_KEY_GEN not supported ===========\n");
	} else {
		fprintf(stderr, "\n==================== CKM_TLS_GOST_PRE_MASTER_KEY_GEN test ==================\n");
		rc = test_tls_pre_master_key_gen(hSession);
		if (rc != CKR_OK) {
			fprintf(stderr, "ERROR CKM_TLS_GOST_PRE_MASTER_KEY_GEN failed, rc = %p\n", (void *)rc);
		} else {
			fprintf(stderr, "CKM_TLS_GOST_PRE_MASTER_KEY_GEN 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_pre_master_key_gen(CK_SESSION_HANDLE sess)
{
	int rc = 0;
	CK_BYTE value[1024];
	CK_ULONG i;
   	CK_MECHANISM mechanism_desc = 
		{CKM_TLS_GOST_PRE_MASTER_KEY_GEN, 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;
	static CK_BBOOL lfalse = CK_FALSE;
	CK_ATTRIBUTE attr;
	static CK_ATTRIBUTE ltemplate[] = {
		{ CKA_CLASS, &oclass, sizeof(oclass) },
		{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
		{ CKA_SENSITIVE, &lfalse, sizeof(lfalse) },
		{ CKA_EXTRACTABLE, &ltrue, sizeof(ltrue) },
		{ CKA_DERIVE, &ltrue, sizeof(ltrue) },
		{ CKA_SIGN, &ltrue, sizeof(ltrue) },
		{ CKA_VERIFY, &ltrue, sizeof(ltrue) }
	};
    CK_VERSION ver; // TLS version

    ver.major = 1;
    ver.minor = 0;
    mechanism->pParameter = &ver;
    mechanism->ulParameterLen = sizeof(ver);
	rc = funcs->C_GenerateKey(sess, mechanism,
			ltemplate, sizeof(ltemplate)/sizeof(CK_ATTRIBUTE), 
			&keyh);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GenerateKey failed: 0x%x\n", rc);
		return rc;
	}

	attr.type = CKA_VALUE; 
	attr.pValue = value; 
	attr.ulValueLen = 32;
	rc = funcs->C_GetAttributeValue(sess, keyh, &attr, 1);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GetAttributeValue failed: 0x%x\n", rc);
		return rc;
	}

	printf("\n %%%%%% KEY: ");
	for (i = 0; i < attr.ulValueLen; i++) printf(" %02x", value[i]);
	printf(" %%%%%%\n\n");

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

	return rc;
}



