#include "test_common.h"

CK_UTF8CHAR *initial_user_pin = "11112222";
CK_UTF8CHAR *new_user_pin = "22222222";
CK_UTF8CHAR *new_so_pin = "88888888";
CK_UTF8CHAR *sopin = NULL;
CK_UTF8CHAR *userpin = NULL;

CK_RV find_objects(CK_SESSION_HANDLE sess, char must_be_found);
CK_RV create_some_objects(CK_SESSION_HANDLE sess);
CK_RV remove_objects(CK_SESSION_HANDLE sess);

int main(int argc, char *argv[]){

	CK_RV rc; // Return Code

#ifndef MANUAL_INIT_TOKEN_TEST
	CK_FLAGS flags = 0; // Bit mask for what options were passed in
	CK_SESSION_HANDLE sess;
#endif
	int i;
	CK_UTF8CHAR *label = "Token Label";
	CK_UTF8CHAR label32[32];

	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];
		}
	}

	//сначала всё сотрем

	printf("==>> init...\n");
	rc = init(api_path, &funcs, NULL);
	if (rc != CKR_OK) {
		printf("==>> init failed\n\n");
		return rc;
	}
	printf("==>> init Ok\n\n");

	printf("==>> get_slot_list...\n");
	rc = get_slot_list(funcs,
                    &SlotList,
                    &SlotCount);
	if (rc != CKR_OK) {
		printf("==>> get_slot_list failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> get_slot_list Ok\n\n");

	printf("==>> find_slot_with_token...\n");
	rc = find_slot_with_token(funcs,
                           SlotList,
                           SlotCount,
                           &SlotId);
	if (rc != CKR_OK) {
		printf("==>> find_slot_with_token failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_slot_with_token Ok\n\n");

	userpin = user_pin;
	sopin = so_pin;

	printf("==>> C_InitToken...\n");
	memset(label32, ' ', sizeof(label32));
	memcpy(label32, label, strlen(label));  
	rc = funcs->C_InitToken(SlotId, sopin, strlen(sopin), label32);
	if (rc != CKR_OK) {
		printf("==>> C_InitToken failed\n\n");
		return rc;
	}
	printf("==>> C_InitToken Ok\n\n");

#ifdef MANUAL_INIT_TOKEN_TEST
	printf("==>> test format Ok\n");
	return rc;
#else
	printf("==>> C_Finalize...\n");
	if(SlotList){free (SlotList);SlotList=NULL;}
	rc = funcs->C_Finalize(NULL);
	if (rc != CKR_OK) {
		printf("==>> C_Finalize failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Finalize Ok\n\n");

	// начинаем тест

	printf("*** No objects must be here:\n\n");

	printf("==>> C_Initialize...\n");
	rc = funcs->C_Initialize(NULL);
	if (rc != CKR_OK) {
		printf("==>> C_Initialize, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Initialize Ok\n\n");

	printf("==>> get_slot_list...\n");
	rc = get_slot_list(funcs,
                    &SlotList,
                    &SlotCount);
	if (rc != CKR_OK) {
		printf("==>> get_slot_list failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> get_slot_list Ok\n\n");

	printf("==>> find_slot_with_token...\n");
	rc = find_slot_with_token(funcs,
                           SlotList,
                           SlotCount,
                           &SlotId);
	if (rc != CKR_OK) {
		printf("==>> find_slot_with_token failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_slot_with_token Ok\n\n");

	printf("==>> C_OpenSession...\n");
	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\n", rc );
		return rc;
	}
	printf("==>> C_OpenSession Ok\n\n");

	printf("==>> C_Login (CKU_USER)...\n");
	rc = funcs->C_Login(sess, CKU_USER, userpin, strlen(userpin));
	if (rc != CKR_OK) {
		printf("==>> C_Login failed, rc = 0x%x. --> It is Ok <--\n\n", rc );
	} else {
		// После инициализации токена пользовательский логин
		// не должен быть успешным, потому что пользовательский PIN
		// не установлен.
		printf("==>> C_Login (CKU_USER) success. It is wrong\n\n");
		return rc;
	}

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

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

	printf("==>> C_Logout (CKU_SO)...\n");
	rc = funcs->C_Logout(sess);
	if (rc != CKR_OK) {
		printf("==>> C_Logout failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Logout (CKU_SO) success\n\n");

	// Не все токены позволяют установить USER PIN без логина
	printf("==>> C_Login (CKU_USER)...\n");
	rc = funcs->C_Login(sess, CKU_USER, initial_user_pin, strlen(initial_user_pin));
	if (rc != CKR_OK) {
		printf("==>> C_Login failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Login (CKU_USER) success\n\n");

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

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

	printf("==>> C_Logout (CKU_SO)...\n");
	rc = funcs->C_Logout(sess);
	if (rc != CKR_OK) {
		printf("==>> C_Logout failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Logout (CKU_SO) success\n\n");

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

	printf("==>> find_objects...\n");
	rc = find_objects(sess, 0);
	if (rc != CKR_OK) {
		if(rc==CKR_GENERAL_ERROR)
		printf("==>> find_objects failed - objects found\n\n");
		else
		printf("==>> find_objects failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_objects success - no objects found\n\n");

	printf("==>> create_some_objects...\n");
	rc = create_some_objects(sess);
	if (rc != CKR_OK) {
		printf("==>> create_some_objects, rc = 0x%x\n", rc );
		return rc;
	}
	printf("==>> create_some_objects success\n\n");

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

	printf("==>> C_CloseSession...\n");
	rc = funcs->C_CloseSession(sess);
	if (rc != CKR_OK) {
		printf("==>> C_CloseSession failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_CloseSession Ok\n\n");

	printf("==>> C_Finalize...\n");
	if(SlotList){free (SlotList);SlotList=NULL;}
	rc = funcs->C_Finalize(NULL);
	if (rc != CKR_OK) {
		printf("==>> C_Finalize failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Finalize Ok\n\n");

	printf("*** Some objects must be here:\n\n");

	printf("==>> C_Initialize...\n");
	rc = funcs->C_Initialize(NULL);
	if (rc != CKR_OK) {
		printf("==>> C_Initialize, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Initialize Ok\n\n");

	printf("==>> get_slot_list...\n");
	rc = get_slot_list(funcs,
                    &SlotList,
                    &SlotCount);
	if (rc != CKR_OK) {
		printf("==>> get_slot_list failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> get_slot_list Ok\n\n");

	printf("==>> find_slot_with_token...\n");
	rc = find_slot_with_token(funcs,
                           SlotList,
                           SlotCount,
                           &SlotId);
	if (rc != CKR_OK) {
		printf("==>> find_slot_with_token failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_slot_with_token Ok\n\n");

	printf("==>> C_OpenSession...\n");
	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\n", rc );
		return rc;
	}
	printf("==>> C_OpenSession Ok\n\n");

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

	printf("==>> find_objects...\n");
	rc = find_objects(sess, 1);
	if (rc != CKR_OK) {
		if(rc==CKR_GENERAL_ERROR)
		printf("==>> find_objects failed - objects not found\n\n");
		else
		printf("==>> find_objects failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_objects success - objects found\n\n");


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

	printf("==>> C_CloseSession...\n");
	rc = funcs->C_CloseSession(sess);
	if (rc != CKR_OK) {
		printf("==>> C_CloseSession failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_CloseSession Ok\n\n");

	printf("==>> C_InitToken...\n");
	memset(label32, ' ', sizeof(label32));
	memcpy(label32, label, strlen(label));  
	rc = funcs->C_InitToken(SlotId, sopin, strlen(sopin), label32);
	if (rc != CKR_OK) {
		printf("==>> C_InitToken failed\n\n");
		return rc;
	}
	printf("==>> C_InitToken Ok\n\n");

	printf("==>> C_Finalize...\n");
	if(SlotList){free (SlotList);SlotList=NULL;}
	rc = funcs->C_Finalize(NULL);
	if (rc != CKR_OK) {
		printf("==>> C_Finalize failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Finalize Ok\n\n");

	printf("*** No objects must be here:\n\n");

	printf("==>> C_Initialize...\n");
	rc = funcs->C_Initialize(NULL);
	if (rc != CKR_OK) {
		printf("==>> C_Initialize, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Initialize Ok\n\n");

	printf("==>> get_slot_list...\n");
	rc = get_slot_list(funcs,
                    &SlotList,
                    &SlotCount);
	if (rc != CKR_OK) {
		printf("==>> get_slot_list failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> get_slot_list Ok\n\n");

	printf("==>> find_slot_with_token...\n");
	rc = find_slot_with_token(funcs,
                           SlotList,
                           SlotCount,
                           &SlotId);
	if (rc != CKR_OK) {
		printf("==>> find_slot_with_token failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_slot_with_token Ok\n\n");

	printf("==>> C_OpenSession...\n");
	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\n", rc );
		return rc;
	}
	printf("==>> C_OpenSession Ok\n\n");

	printf("==>> C_Login (CKU_USER)...\n");
	rc = funcs->C_Login(sess, CKU_USER, userpin, strlen(userpin));
	if (rc != CKR_OK) {
		printf("==>> C_Login failed, rc = 0x%x. --> It is Ok <--\n\n", rc );
	} else {
		printf("==>> C_Login (CKU_USER) success. It is wrong\n\n");
		return rc;
	}

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

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

	printf("==>> C_Logout (CKU_SO)...\n");
	rc = funcs->C_Logout(sess);
	if (rc != CKR_OK) {
		printf("==>> C_Logout failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Logout (CKU_SO) success\n\n");

	// Не все токены позволяет устанавливать пользовательский PIN без логина
	printf("==>> C_Login (CKU_USER)...\n");
	rc = funcs->C_Login(sess, CKU_USER, initial_user_pin, strlen(initial_user_pin));
	if (rc != CKR_OK) {
		printf("==>> C_Login failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Login (CKU_USER) success\n\n");

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

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

	printf("==>> C_Logout (CKU_SO)...\n");
	rc = funcs->C_Logout(sess);
	if (rc != CKR_OK) {
		printf("==>> C_Logout failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_Logout (CKU_SO) success\n\n");

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

	printf("==>> find_objects...\n");
	rc = find_objects(sess, 0);
	if (rc != CKR_OK) {
		if(rc==CKR_GENERAL_ERROR)
		printf("==>> find_objects failed - objects found\n\n");
		else
		printf("==>> find_objects failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> find_objects success - no objects found\n\n");

	printf("==>> create_some_objects (final check) ...\n");
	rc = create_some_objects(sess);
	if (rc != CKR_OK) {
		printf("==>> create_some_objects (final check) , rc = 0x%x\n", rc );
		return rc;
	}
	printf("==>> create_some_objects (final check) success\n\n");

	printf("==>> remove_objects ...\n");
	rc = remove_objects(sess);
	if (rc != CKR_OK) {
		printf("==>> remove_objects, rc = 0x%x\n", rc );
		return rc;
	}
	printf("==>> remove_objects success\n\n");

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

	printf("==>> C_CloseSession...\n");
	rc = funcs->C_CloseSession(sess);
	if (rc != CKR_OK) {
		printf("==>> C_CloseSession failed, rc = 0x%x\n\n", rc );
		return rc;
	}
	printf("==>> C_CloseSession Ok\n\n");

	printf("==>> test format Ok\n");

	return rc;
#endif

}

CK_RV create_some_objects(CK_SESSION_HANDLE sess){
	CK_RV rc; // Return Code
	CK_FLAGS flags = 0; // Bit mask for what options were passed in
	//сертификат
	static CK_OBJECT_CLASS oclass_cert = CKO_CERTIFICATE;
	static CK_CERTIFICATE_TYPE ocert_type = CKC_X_509;
	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";
	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_TOKEN, &ltrue, sizeof(ltrue)},
		{CKA_SUBJECT, subject, strlen(subject)+1},
		{CKA_LABEL, label, strlen(label)+1},
		{CKA_VALUE, der_value, sizeof(der_value)},
	};
	CK_ULONG count=0;
	CK_OBJECT_HANDLE hObj = CK_INVALID_HANDLE;
	//пара
//	CK_BYTE value[1024];
	CK_OBJECT_HANDLE pub_key = CK_INVALID_HANDLE;
	CK_OBJECT_HANDLE priv_key = CK_INVALID_HANDLE;
//	CK_ATTRIBUTE attr;
   	CK_MECHANISM mechanism_desc = {CKM_GOSTR3410_KEY_PAIR_GEN, NULL, 0};
   	CK_MECHANISM_PTR mechanism = &mechanism_desc;
//	CK_MECHANISM_INFO minfo;

	static CK_BYTE gostR3410params_A[] = {
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 };
	static CK_BYTE gostR3411params[] = {
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01 };

	static CK_BYTE data_hash[] = {
		0xF4,0x21,0x15,0x4E,0x51,0x08,0x09,0xC9,
		0x41,0xD8,0xF3,0xAF,0xA1,0x2E,0x51,0xDB,
		0x22,0x44,0x5D,0x2E,0xC9,0x91,0x91,0x2E,
		0x6E,0xBA,0x91,0xE8,0x7C,0xFE,0xF5,0xD1
	};
	static CK_ATTRIBUTE pub_template[] = {
		{CKA_TOKEN, &ltrue, sizeof(CK_BBOOL)},
		{CKA_GOSTR3410PARAMS, gostR3410params_A, sizeof(gostR3410params_A)},
		{CKA_GOSTR3411PARAMS, gostR3411params, sizeof(gostR3411params)},
	};
	static CK_ATTRIBUTE priv_template[] = {
		{CKA_TOKEN, &ltrue, sizeof(CK_BBOOL)},
		{CKA_PRIVATE, &ltrue, sizeof(CK_BBOOL)},
	};
	//создаем сертификат
	rc = funcs->C_CreateObject(sess,
		cert_create, sizeof(cert_create)/sizeof(CK_ATTRIBUTE), &hObj);
	if (rc != CKR_OK) {
		printf("C_CreateObject (CERT) failed, rc = 0x%x\n", rc );
		return rc;
	}
	printf("C_CreateObject (CERT) success\n", count);
/*
	// ГОСТ не всеми токенами поддерживается
	rc = funcs->C_GetMechanismInfo(SlotId, CKM_GOSTR3410_KEY_PAIR_GEN, &minfo);
	if (rc != CKR_OK) {
		printf("\n======== Mechanism CKM_GOSTR3410_KEY_PAIR_GEN not supported ===========\n");
	} else {
		//генерим пару
		rc = funcs->C_GenerateKeyPair(sess, mechanism,
				pub_template, sizeof(pub_template)/sizeof(CK_ATTRIBUTE),
				priv_template, sizeof(priv_template)/sizeof(CK_ATTRIBUTE),
				&pub_key, &priv_key);
		if (rc != CKR_OK) {
			printf("C_GenerateKeyPair failed, rc = 0x%x\n", rc);
		} else {
			attr.type = CKA_VALUE;
			attr.pValue = value;
			attr.ulValueLen = sizeof(value);
			rc = funcs->C_GetAttributeValue(sess, pub_key, &attr, 1);
			if (rc != CKR_OK) {
				printf("C_GetAttributeValue failed, rc = 0x%x\n", rc);
			}
		}
		if (rc != CKR_OK) {
			printf("CKM_GOSTR3410_KEY_PAIR_GEN failed, rc = 0x%x\n", rc);
		} else {
			printf("CKM_GOSTR3410_KEY_PAIR_GEN success.\n");
		}
	}
*/
	return CKR_OK;
}

CK_RV remove_objects(CK_SESSION_HANDLE sess) {
	CK_RV rc = CKR_OK;
	CK_OBJECT_HANDLE  obj_list[20];
	CK_ULONG          find_count;
	CK_ULONG          i;

	rc = funcs->C_FindObjectsInit(sess, NULL, 0 );
	if (rc != CKR_OK) {
		printf("C_FindObjectsInit: 0x%x\n", rc );
		goto done;
	}

	do {
		rc = funcs->C_FindObjects( sess, obj_list, 20, &find_count );
		if (rc != CKR_OK) {
			printf("C_FindObjects: 0x%x\n", rc );
			goto done;
		}

		for (i=0; i < find_count; i++) {
			rc = funcs->C_DestroyObject( sess, obj_list[i] );
			if (rc != CKR_OK) {
				printf("C_DestroyObject #%ld: 0x%x\n", i, rc);
			}
		}
	} while (find_count != 0);

	rc = funcs->C_FindObjectsFinal( sess );
	if (rc != CKR_OK) {
		printf("C_FindObjectsFinal: 0x%x\n", rc );
		goto done;
	}
done:
	return rc;
}

CK_RV find_objects(CK_SESSION_HANDLE sess, char must_be_found){
	CK_RV rc; // Return Code
	//сначала сертификат
	{
		CK_ULONG number = 0;
		CK_OBJECT_HANDLE hObj = CK_INVALID_HANDLE;
		CK_OBJECT_CLASS oclass_cert = CKO_CERTIFICATE;
		CK_ATTRIBUTE cert_find[] = {
			{CKA_CLASS, &oclass_cert, sizeof(oclass_cert)},
		};

		printf("CERT: C_FindObjectsInit ...\n");
		rc = funcs->C_FindObjectsInit(sess, cert_find, sizeof(cert_find)/sizeof(CK_ATTRIBUTE));
		if (rc != CKR_OK) {
			printf("CERT: C_FindObjectsInit failed, rc = 0x%x\n\n", rc );
			return rc;
		}
		printf("CERT: C_FindObjectsInit Ok\n\n");

		printf("CERT: C_FindObjects...\n");
		rc = funcs->C_FindObjects(sess, &hObj, 1, &number);
		if (rc != CKR_OK) {
			printf("CERT: C_FindObjects failed, rc = 0x%x\n\n", rc );
			return rc;
		}

		if(must_be_found){
			if(hObj != CK_INVALID_HANDLE){
				printf("CERT: C_FindObjects : found - Ok\n\n");
			} else {
				printf("CERT: C_FindObjects : not found - Wrong\n\n");
				return CKR_GENERAL_ERROR;
			}
		} else {
			if(hObj != CK_INVALID_HANDLE){
				printf("CERT: C_FindObjects : found - Wrong\n\n");
				return CKR_GENERAL_ERROR;
			} else {
				printf("CERT: C_FindObjects : not found -  Ok\n\n");
			}
		}

		printf("CERT: C_FindObjectsFinal...\n");
		rc = funcs->C_FindObjectsFinal(sess);
		if (rc != CKR_OK) {
			printf("CERT: C_FindObjectsFinal failed, rc = 0x%x\n\n", rc );
			return rc;
		}
		printf("CERT: C_FindObjectsFinal Ok\n\n");
	}
/*
	//теперь ключи
	{
		CK_OBJECT_HANDLE pub_key = CK_INVALID_HANDLE;
		CK_OBJECT_HANDLE priv_key = CK_INVALID_HANDLE;
		CK_BBOOL ltrue = CK_TRUE;
		CK_BBOOL lfalse = CK_FALSE;
		CK_OBJECT_CLASS oclass_pub = CKO_PUBLIC_KEY;
		CK_OBJECT_CLASS oclass_priv = CKO_PRIVATE_KEY;
		CK_KEY_TYPE key_type = CKK_GOSTR3410;

		CK_ATTRIBUTE find_priv[] = {
			{ CKA_CLASS, &oclass_priv, sizeof(CK_OBJECT_CLASS) },
			{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
			{ CKA_TOKEN, &ltrue, sizeof(ltrue)},
		};
		CK_ATTRIBUTE find_pub[] = {
			{ CKA_CLASS, &oclass_pub, sizeof(CK_OBJECT_CLASS) },
			{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
			{ CKA_TOKEN, &ltrue, sizeof(ltrue)},
		};
		CK_ULONG number = 0;

		printf("PRIV_KEY: C_FindObjectsInit...\n");
		rc = funcs->C_FindObjectsInit(sess, find_priv, sizeof(find_priv)/sizeof(CK_ATTRIBUTE));
		if (rc != CKR_OK) {
			printf("PRIV_KEY: C_FindObjectsInit failed: 0x%x\n\n", rc);
			return rc;
		}
		printf("PRIV_KEY: C_FindObjectsInit OK\n\n");

		printf("PRIV_KEY: C_FindObjects...\n");
		rc = funcs->C_FindObjects(sess, &priv_key, 1, &number);
		if (rc != CKR_OK) {
			printf("PRIV_KEY: C_FindObjects failed: 0x%x\n\n", rc);
			return rc;
		}

		if(must_be_found){
			if(priv_key != CK_INVALID_HANDLE){
				printf("PRIV_KEY: C_FindObjects : found - Ok\n\n");
			} else {
				printf("PRIV_KEY: C_FindObjects : not found - Wrong\n\n");
				return CKR_GENERAL_ERROR;
			}
		} else {
			if(priv_key != CK_INVALID_HANDLE){
				printf("PRIV_KEY: C_FindObjects : found - Wrong\n\n");
				return CKR_GENERAL_ERROR;
			} else {
				printf("PRIV_KEY: C_FindObjects : not found -  Ok\n\n");
			}
		}

		printf("PRIV_KEY: C_FindObjectsFinal...\n");
		rc = funcs->C_FindObjectsFinal(sess);
		if (rc != CKR_OK) {
			printf("PRIV_KEY: C_FindObjectsFinal failed: 0x%x\n\n", rc);
			return rc;
		}
		printf("PRIV_KEY: C_FindObjectsFinal OK\n\n");

		printf("PUB_KEY: C_FindObjectsInit...\n");
		rc = funcs->C_FindObjectsInit(sess, find_pub, sizeof(find_pub)/sizeof(CK_ATTRIBUTE));
		if (rc != CKR_OK) {
			printf("PUB_KEY: C_FindObjectsInit failed: 0x%x\n\n", rc);
			return rc;
		}
		printf("PUB_KEY: C_FindObjectsInit OK\n\n");

		printf("PUB_KEY: C_FindObjects...\n");
		rc = funcs->C_FindObjects(sess, &pub_key, 1, &number);
		if (rc != CKR_OK) {
			printf("PUB_KEY: C_FindObjects failed: 0x%x\n\n", rc);
			return rc;
		}

		if(must_be_found){
			if(pub_key != CK_INVALID_HANDLE){
				printf("PUB_KEY: C_FindObjects : found - Ok\n\n");
			} else {
				printf("PUB_KEY: C_FindObjects : not found - Wrong\n\n");
				return CKR_GENERAL_ERROR;
			}
		} else {
			if(pub_key != CK_INVALID_HANDLE){
				printf("PUB_KEY: C_FindObjects : found - Wrong\n\n");
				return CKR_GENERAL_ERROR;
			} else {
				printf("PUB_KEY: C_FindObjects : not found -  Ok\n\n");
			}
		}

		printf("PUB_KEY: C_FindObjectsFinal...\n");
		rc = funcs->C_FindObjectsFinal(sess);
		if (rc != CKR_OK) {
			printf("PUB_KEY: C_FindObjectsFinal failed: 0x%x\n\n", rc);
			return rc;
		}
		printf("PUB_KEY: C_FindObjectsFinal OK\n\n");
	}
*/
	return CKR_OK;
}

