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

void usage(void);
CK_RV test_crypto();
int test_pba_gost3411_with_gost3411_hmac(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_PBA_GOSTR3411_WITH_GOSTR3411_HMAC, &minfo);
	if (rc != CKR_OK) {
		fprintf(stderr, "\n======== Mechanism CKM_PBA_GOSTR3411_WITH_GOSTR3411_HMAC not supported ===========\n");
	} else {
		fprintf(stderr, "\n==================== CKM_PBA_GOSTR3411_WITH_GOSTR3411_HMAC test ==================\n");
		rc = test_pba_gost3411_with_gost3411_hmac(hSession);
		if (rc != CKR_OK) {
			fprintf(stderr, "ERROR CKM_PBA_GOSTR3411_WITH_GOSTR3411_HMAC failed, rc = %p\n", (void *)rc);
		} else {
			fprintf(stderr, "CKM_PBA_GOSTR3411_WITH_GOSTR3411_HMAC 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_pba_gost3411_with_gost3411_hmac(CK_SESSION_HANDLE sess)
{
	int rc = 0;
	CK_BYTE value[256];
	CK_ULONG len;
   	CK_MECHANISM mechanism_desc = 
		{CKM_PBA_GOSTR3411_WITH_GOSTR3411_HMAC, NULL, 0};
   	CK_MECHANISM_PTR mechanism = &mechanism_desc;
	CK_OBJECT_HANDLE key = CK_INVALID_HANDLE;
	static CK_BBOOL ltrue = CK_TRUE;
	static CK_BBOOL lfalse = CK_FALSE;
	CK_GOSTR3411_PBE_PARAMS params;
	CK_ATTRIBUTE attr;
    CK_KEY_TYPE key_type = 0;
	// CryptoPro gostR3411 A Param Set
	static CK_BYTE oid_default[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01,
	};
    // Real PKCS#12 params and ethalon
	static CK_BYTE salt4[] = { 
		0xDF, 0x7C, 0x5C, 0x10, 0xA4, 0xD5, 0x22, 0x62,
	};
	static CK_ULONG iter4 = 2048;
    // Use UTF-8 password here
    // Converting from UTF-8 to UTF-16 in soft token
    static CK_UTF8CHAR password4[] = {0x34, 0x34, 0x34, 0x34};  
	static CK_ULONG password4_len = 4;
	static CK_BYTE et14[] = { 
		0x93, 0x88, 0x91, 0x11, 0x20, 0x43, 0xC4, 0xD1,                             
		0xCA, 0x23, 0x82, 0xEF, 0x86, 0x4A, 0x67, 0x31,                              
		0xBD, 0x29, 0xEF, 0x82, 0x94, 0xDD, 0x23, 0x50,                               
		0x63, 0x0B, 0xCB, 0x1E, 0x5A, 0xC9, 0x06, 0xC3,
	};
	static CK_ATTRIBUTE key_template[] = {
		{ CKA_SENSITIVE, &lfalse, sizeof(lfalse) },
		{ CKA_EXTRACTABLE, &ltrue, sizeof(ltrue) }
	};

	params.pOID = oid_default;
    params.ulOIDLen = sizeof(oid_default);
	params.pSalt = salt4;
	params.ulSaltLen = sizeof(salt4);
	params.ulIteration = iter4;
	params.pPassword = password4;
	params.ulPasswordLen = password4_len;
	mechanism->pParameter = &params;
	mechanism->ulParameterLen = sizeof(params);
	// Механизм CKM_PBA_GOSTR3411_WITH_GOSTR3411_HMAC вырабатывает 
	// на пароле так называемый ключ аутентификации, 
	// значение которого используется для проверки целостности
	// транспортного контейнера PKCS#12.
	rc = funcs->C_GenerateKey(sess, mechanism, 
		key_template, sizeof(key_template)/sizeof(CK_ATTRIBUTE), &key);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GenerateKey failed: 0x%x\n", rc);
        goto end;
	}
	attr.type = CKA_KEY_TYPE; 
	attr.pValue = &key_type; 
	attr.ulValueLen = sizeof(key_type);
	rc = funcs->C_GetAttributeValue(sess, key, &attr, 1);
	len = attr.ulValueLen;
    if (key_type != CKK_GENERIC_SECRET) {
        fprintf(stderr, 
			"Key type 0x%x != CKK_GENERIC_SECRET\n", key_type);
		rc = -1;
        goto end;
    }
	attr.type = CKA_VALUE; 
	attr.pValue = value; 
	attr.ulValueLen = sizeof(value);
	rc = funcs->C_GetAttributeValue(sess, key, &attr, 1);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GetAttributeValue failed: 0x%x\n", rc);
        goto end;
	}
	len = attr.ulValueLen;
	if (len != sizeof(et14)) {
		fprintf(stderr, "Invalid key length: %d\n", len);
		rc = -2;
		goto end;
	}
	print_hex(value, len);
	if (memcmp(value, et14, len) != 0) {
		fprintf(stderr, "Invalid key value\n");
		rc = -3;
		goto end;
	}

	printf("SUCCESS\n");
	rc = CKR_OK;
end:
	if (key != CK_INVALID_HANDLE) {
		funcs->C_DestroyObject(sess, key);
	}
	return rc;
}



