#include "test_common.h"

CK_SLOT_ID SlotId;

CK_SLOT_ID_PTR SlotList = NULL;
CK_ULONG SlotCount = 0;

int
main(int argc, char *argv[]){
	CK_RV rc; // Return Code
	CK_FLAGS flags = 0; // Bit mask for what options were passed in
	CK_SESSION_HANDLE sess;
	CK_ULONG i;
	static CK_OBJECT_CLASS oclass_cert = CKO_CERTIFICATE;
	static CK_CERTIFICATE_TYPE ocert_type = CKC_X_509;
	static CK_ULONG ocert_category = 0;
	static CK_BBOOL ltrue = CK_TRUE;
	static CK_BBOOL lfalse = CK_FALSE;
	CK_ATTRIBUTE cert_find[] = {
		{CKA_CLASS, &oclass_cert, sizeof(oclass_cert)},
	};
	CK_UTF8CHAR label[] = "cpcacert";
	CK_CHAR subject[] = "CryptoPro CA";
	// Размер сертификата - 583 байта
	CK_BYTE der_value[] = {
		0x30, 0x82, 0x02, 0x43, 0x30, 0x82, 0x01, 0xF0, 
		0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x69,
		0x84, 0x03, 0x28, 0x6A, 0xA6, 0x59, 0xBA, 0x46, 
		0x35, 0x62, 0x2D, 0x49, 0xDE, 0x5F, 0xD3, 0x30,
		0x0A, 0x06, 0x06, 0x2A, 0x85, 0x03, 0x02, 0x02, 
		0x03, 0x05, 0x00, 0x30, 0x65, 0x31, 0x20, 0x30, 
		0x1E, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 
		0x0D, 0x01, 0x09, 0x01, 0x16, 0x11, 0x69, 0x6E, 
		0x66, 0x6F, 0x40, 0x63, 0x72, 0x79, 0x70, 0x74, 
		0x6F, 0x70, 0x72, 0x6F, 0x2E, 0x72, 0x75, 0x31, 
		0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 
		0x13, 0x02, 0x52, 0x55, 0x31, 0x13, 0x30, 0x11, 
		0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0A, 0x43, 
		0x52, 0x59, 0x50, 0x54, 0x4F, 0x2D, 0x50, 0x52, 
		0x4F, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x03, 0x55, 
		0x04, 0x03, 0x13, 0x16, 0x54, 0x65, 0x73, 0x74, 
		0x20, 0x43, 0x65, 0x6E, 0x74, 0x65, 0x72, 0x20, 
		0x43, 0x52, 0x59, 0x50, 0x54, 0x4F, 0x2D, 0x50, 
		0x52, 0x4F, 0x30, 0x1E, 0x17, 0x0D, 0x30, 0x39, 
		0x30, 0x34, 0x30, 0x37, 0x31, 0x32, 0x30, 0x32, 
		0x31, 0x35, 0x5A, 0x17, 0x0D, 0x31, 0x34, 0x31, 
		0x30, 0x30, 0x34, 0x30, 0x37, 0x30, 0x39, 0x34, 
		0x31, 0x5A, 0x30, 0x65, 0x31, 0x20, 0x30, 0x1E, 
		0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 
		0x01, 0x09, 0x01, 0x16, 0x11, 0x69, 0x6E, 0x66, 
		0x6F, 0x40, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6F, 
		0x70, 0x72, 0x6F, 0x2E, 0x72, 0x75, 0x31, 0x0B, 
		0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 
		0x02, 0x52, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 
		0x03, 0x55, 0x04, 0x0A, 0x13, 0x0A, 0x43, 0x52, 
		0x59, 0x50, 0x54, 0x4F, 0x2D, 0x50, 0x52, 0x4F, 
		0x31, 0x1F, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x04, 
		0x03, 0x13, 0x16, 0x54, 0x65, 0x73, 0x74, 0x20, 
		0x43, 0x65, 0x6E, 0x74, 0x65, 0x72, 0x20, 0x43, 
		0x52, 0x59, 0x50, 0x54, 0x4F, 0x2D, 0x50, 0x52, 
		0x4F, 0x30, 0x63, 0x30, 0x1C, 0x06, 0x06, 0x2A, 
		0x85, 0x03, 0x02, 0x02, 0x13, 0x30, 0x12, 0x06, 
		0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01, 
		0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x1E, 
		0x01, 0x03, 0x43, 0x00, 0x04, 0x40, 0x02, 0xE4, 
		0xFF, 0xD1, 0xA6, 0xF6, 0x9C, 0x80, 0x9A, 0xDA, 
		0xEC, 0x7F, 0x4A, 0x78, 0xC1, 0xCC, 0x2D, 0xD3, 
		0xE5, 0x96, 0xEA, 0xCB, 0xED, 0x22, 0x32, 0x79, 
		0xB2, 0x02, 0xE2, 0xC6, 0x7C, 0x35, 0xE6, 0x74, 
		0x64, 0x1B, 0x09, 0x77, 0x11, 0x8C, 0x67, 0x3F, 
		0x0F, 0xD0, 0xE8, 0x23, 0xA6, 0x7E, 0x6D, 0x3B, 
		0x7F, 0xC4, 0xC4, 0x28, 0xFD, 0x2B, 0x1C, 0x68, 
		0x01, 0x20, 0xA0, 0x5C, 0xD8, 0x79, 0xA3, 0x78, 
		0x30, 0x76, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x1D, 
		0x0F, 0x04, 0x04, 0x03, 0x02, 0x01, 0xC6, 0x30, 
		0x0F, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 
		0xFF, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 
		0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 
		0x16, 0x04, 0x14, 0x6D, 0x8F, 0x5E, 0x05, 0xD9, 
		0x5F, 0xAC, 0x91, 0x17, 0x94, 0x1E, 0x95, 0x9A, 
		0x05, 0x30, 0x38, 0x37, 0x7A, 0x10, 0x2A, 0x30, 
		0x12, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 
		0x82, 0x37, 0x15, 0x01, 0x04, 0x05, 0x02, 0x03, 
		0x02, 0x00, 0x02, 0x30, 0x23, 0x06, 0x09, 0x2B, 
		0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x15, 0x02, 
		0x04, 0x16, 0x04, 0x14, 0x7A, 0xC9, 0xC7, 0x09, 
		0xDB, 0x20, 0x1C, 0x96, 0x94, 0x2F, 0xFC, 0x46, 
		0xAD, 0x6D, 0x93, 0xD0, 0x5E, 0x69, 0x12, 0x0E, 
		0x30, 0x0A, 0x06, 0x06, 0x2A, 0x85, 0x03, 0x02, 
		0x02, 0x03, 0x05, 0x00, 0x03, 0x41, 0x00, 0x58, 
		0x73, 0xD2, 0x93, 0xBC, 0x63, 0x21, 0xB1, 0x0E, 
		0x73, 0x72, 0xEE, 0xF1, 0x72, 0xB5, 0x1B, 0x8B, 
		0xBB, 0xC9, 0x3B, 0x08, 0xBB, 0x4C, 0x5A, 0xF2, 
		0xE1, 0xA5, 0x35, 0x4F, 0x99, 0xC4, 0xD5, 0x52, 
		0x52, 0x70, 0x26, 0xDD, 0xAE, 0xD0, 0xA9, 0x27, 
		0xE9, 0xB6, 0x5B, 0x7D, 0x6F, 0x44, 0xFD, 0x26, 
		0x4D, 0xFD, 0xA1, 0x63, 0x74, 0x5C, 0x74, 0xD8, 
		0x49, 0x73, 0x0A, 0x77, 0x77, 0x63, 0x4D,

	};
	CK_ATTRIBUTE cert_create[] = {
		{CKA_CLASS, &oclass_cert, sizeof(oclass_cert)},
		{CKA_CERTIFICATE_TYPE, &ocert_type, sizeof(ocert_type)},
		{CKA_CERTIFICATE_CATEGORY, &ocert_category, sizeof(ocert_category)}, 
		{CKA_TOKEN, &ltrue, sizeof(ltrue)},
		{CKA_SUBJECT, subject, strlen(subject)+1},
		{CKA_LABEL, label, strlen(label)+1},
		{CKA_VALUE, der_value, sizeof(der_value)},
	};
	CK_BYTE get_value[4096];
	CK_ATTRIBUTE tmpl_value[] = {
		{CKA_VALUE, get_value, sizeof(get_value)},
	};
	CK_ULONG count, number;
	CK_OBJECT_HANDLE hObj = CK_INVALID_HANDLE;
	CK_OBJECT_HANDLE hNewObj = CK_INVALID_HANDLE;
//	CK_ULONG max_number = 12;

	printf("CKO_CERTIFICATE test\n");

	for (i=1; i<(CK_ULONG)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) {
		printf("init failed\n");
		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;
	}


		flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
		rc = funcs->C_OpenSession( SlotId, flags, NULL, NULL, &sess );
		if (rc != CKR_OK) {
			printf("C_OpenSession failed, rc = 0x%x\n", rc );
			return rc;
		}

		rc = funcs->C_Login(sess, CKU_USER, user_pin, strlen(user_pin));
		if (rc != CKR_OK) {
			printf("C_Login failed, rc = 0x%x\n", rc );
			return rc;
		}
		printf("C_Login (CKU_USER) success\n");

		// Ищем все доступные сертификаты
		rc = funcs->C_FindObjectsInit(sess, cert_find, sizeof(cert_find)/sizeof(CK_ATTRIBUTE)); 
		if (rc != CKR_OK) {
			printf("C_FindObjectsFinal failed, rc = 0x%x\n", rc );
			return rc;
		}
		count = 0;
		do {
			number = 0;
			hObj = CK_INVALID_HANDLE;
			rc = funcs->C_FindObjects(sess, &hObj, 1, &number);
			if (rc != CKR_OK) {
				printf("C_FindObjects failed, rc = 0x%x\n", rc );
				return rc;
			}
			if (number > 0 && hObj != CK_INVALID_HANDLE) count ++;
		} while (number > 0 && hObj != CK_INVALID_HANDLE);
		rc = funcs->C_FindObjectsFinal(sess);
		if (rc != CKR_OK) {
			printf("C_FindObjectsFinal failed, rc = 0x%x\n", rc );
			return rc;
		}
		printf("%ld certificates found for session\n", count);

//	for (i=0; i<20; i++) {

		rc = funcs->C_CreateObject(sess, 
			cert_create, sizeof(cert_create)/sizeof(CK_ATTRIBUTE), &hNewObj); 
		if (rc != CKR_OK) {
			printf("C_CreateObject failed, rc = 0x%x\n", rc );
			return rc;
		}
		printf("C_CreateObject success\n");

		rc = funcs->C_GetAttributeValue(sess, hNewObj,
			tmpl_value, sizeof(tmpl_value)/sizeof(CK_ATTRIBUTE)); 
		if (rc != CKR_OK) {
			printf("C_GetAttributeValue failed, rc = 0x%x\n", rc );
			return rc;
		}
		if (tmpl_value[0].ulValueLen != sizeof(der_value)) {
			printf("Invalid CKA_VALUE length\n");
			return -1;
		}
		if (memcmp(get_value, der_value, sizeof(der_value)) != 0 ) {
			printf("Invalid CKA_VALUE\n");
			return -1;
		}
		printf("C_GetAttributeValue success\n");
		// Ищем все доступные сертификаты
		rc = funcs->C_FindObjectsInit(sess, cert_find, sizeof(cert_find)/sizeof(CK_ATTRIBUTE)); 
		if (rc != CKR_OK) {
			printf("C_FindObjectsFinal failed, rc = 0x%x\n", rc );
			return rc;
		}
		count = 0;
		do {
			number = 0;
			hObj = CK_INVALID_HANDLE;
			rc = funcs->C_FindObjects(sess, &hObj, 1, &number);
			if (rc != CKR_OK) {
				printf("C_FindObjects failed, rc = 0x%x\n", rc );
				return rc;
			}
			if (number > 0 && hObj != CK_INVALID_HANDLE) count ++;
		} while (number > 0 && hObj != CK_INVALID_HANDLE);
		rc = funcs->C_FindObjectsFinal(sess);
		if (rc != CKR_OK) {
			printf("C_FindObjectsFinal failed, rc = 0x%x\n", rc );
			return rc;
		}
		printf("%ld certificates found for session\n", count);
		rc = funcs->C_DestroyObject(sess, hNewObj);
		if (rc != CKR_OK) {
			printf("C_DestroyObject failed, rc = 0x%x\n", rc );
			return rc;
		}
//	}
	rc = funcs->C_CloseSession(sess);
	if (rc != CKR_OK) {
		printf("C_CloseSession failed, rc = 0x%x\n", rc );
		return rc;
	}
	printf("CKO_CERTIFICATE test SUCCESS\n");
	return CKR_OK;
}
