std::string DumpToString(const std::vector< uint8_t > & buffer)
{
	return DumpToString(buffer.empty() ? 0 : &buffer[0], static_cast< int32_t >(buffer.size()));
}
Esempio n. 2
0
//Read data
void phAnalyzer::readyRead()
{
	data.resize(socket->bytesAvailable());
	uint64_t count = socket->read(&data[0], socket->bytesAvailable());

	//Write the received data to the end of the stream
	pending_stream.Write<char>(&data[0], count);

	//Total size of stream
	int32_t total_bytes = pending_stream.GetStreamSize();

	//Make sure there are enough bytes for the packet size to be read
	while(total_bytes > 2)
	{
		//Peek the packet size
		uint16_t required_size = pending_stream.Read<uint16_t>(true) + 6;

		//See if there are enough bytes for this packet
		if(required_size <= total_bytes)
		{
			StreamUtility r(pending_stream);

			//Remove this packet from the stream
			pending_stream.Delete(0, required_size);
			pending_stream.SeekRead(0, Seek_Set);
			total_bytes -= required_size;

			//Paused?
			if(ui.chkPause->isChecked())
				continue;

			//Extract packet header information
			uint16_t size = r.Read<uint16_t>();
			uint16_t opcode = r.Read<uint16_t>();
			uint8_t direction = r.Read<uint8_t>();
			uint8_t encrypted = r.Read<uint8_t>();

			//Remove the header
			r.Delete(0, 6);
			r.SeekRead(0, Seek_Set);

			//Player ID
			if(opcode == 0x3020)
			{
				//Have to read it like this otherwise it won't look right
				uint8_t PlayerID0 = r.Read<uint8_t>();
				uint8_t PlayerID1 = r.Read<uint8_t>();
				uint8_t PlayerID2 = r.Read<uint8_t>();
				uint8_t PlayerID3 = r.Read<uint8_t>();

				char temp[16] = {0};
				sprintf_s(temp, "%.2X %.2X %.2X %.2X", PlayerID0, PlayerID1, PlayerID2, PlayerID3);

				ui.PlayerID->setText(temp);
				r.SeekRead(0, Seek_Set);
			}
			//Object selected
			else if(opcode == 0x7045)
			{
				//Have to read it like this otherwise it won't look right
				uint8_t Object0 = r.Read<uint8_t>();
				uint8_t Object1 = r.Read<uint8_t>();
				uint8_t Object2 = r.Read<uint8_t>();
				uint8_t Object3 = r.Read<uint8_t>();

				char temp[16] = {0};
				sprintf_s(temp, "%.2X %.2X %.2X %.2X", Object0, Object1, Object2, Object3);

				ui.SelectedObject->setText(temp);
				r.SeekRead(0, Seek_Set);
			}

			//Check the ignore list
			bool found = false;
			for(int x = 0; x < ui.lstIgnore->count(); ++x)
			{
				bool OK;
				if(ui.lstIgnore->item(x)->text().toUInt(&OK, 16) == opcode)
				{
					found = true;
					break;
				}
			}
			if(found) continue;

			//Check the listen list
			if(ui.lstListen->count())
			{
				found = false;
				for(int x = 0; x < ui.lstListen->count(); ++x)
				{
					bool OK;
					if(ui.lstListen->item(x)->text().toUInt(&OK, 16) == opcode)
					{
						found = true;
						break;
					}
				}

				if(!found)
					continue;
			}

			//Parse the packet for reading
			std::stringstream ss;
			ss << "[" << (direction == 1 ? "C->S" : "S->C") << "] ";
			ss << "[" << size << "] ";
			ss << "[0x" << std::hex << std::setfill('0') << std::setw(4) << opcode << "] " << std::dec;
			ss << (encrypted ? "[E] " : "") << "\n";
			ss << DumpToString(r);

			bool append = false;
			if(direction == 0 && ui.chkServer->isChecked())
			{
				if(ui.chkEncrypted->isChecked() && encrypted)
					append = true;
				if(!ui.chkEncrypted->isChecked())
					append = true;
			}
			else if(direction == 1 && ui.chkClient->isChecked())
			{
				if(ui.chkEncrypted->isChecked() && encrypted)
					append = true;
				if(!ui.chkEncrypted->isChecked())
					append = true;
			}

			//Append the packet to the list
			if(append)
				ui.lstPackets->appendPlainText(ss.str().c_str());
		}
		else
		{
			//Not enough bytes received for this packet
			break;
		}
	}
}
std::string DumpToString(StreamUtility & stream_utility)
{
	return DumpToString(stream_utility.GetStreamVector());
}
Esempio n. 4
0
static
void
Dump (
	IN const CK_FUNCTION_LIST_PTR &pkcs11,
	IN const CK_SLOT_ID slotSlot,
	IN const std::string &strPIN
) {
	CK_RV rv;
	CK_SESSION_HANDLE hSession;
	CK_OBJECT_HANDLE objects[1024];
	CK_ULONG objectsmax = sizeof (objects) / sizeof (CK_OBJECT_HANDLE);
	CK_ULONG objectssize;
	CK_ULONG i;
	CK_SLOT_ID slots[1024];
	CK_ULONG slotsnum;

	if (
		(rv = pkcs11->C_GetSlotList (
			FALSE,
			NULL,
			&slotsnum
		)) != CKR_OK
	) {
		throw CEPKCS11 ("C_GetSlotList", rv);
	}

	if (slotsnum >= sizeof(slots)/sizeof(slots[0])) {
		throw CEPKCS11 ("C_GetSlotList", CKR_BUFFER_TOO_SMALL);
	}

	if (
		(rv = pkcs11->C_GetSlotList (
			FALSE,
			slots,
			&slotsnum
		)) != CKR_OK
	) {
		throw CEPKCS11 ("C_GetSlotList", rv);
	}

	if (
		(rv=pkcs11->C_OpenSession (
			slotSlot,
			CKF_SERIAL_SESSION,
			NULL_PTR,
			NULL_PTR,
			&hSession
		)) != CKR_OK
	) {
		throw CEPKCS11 ("C_OpenSession", rv);
	}

	CK_TOKEN_INFO info;

	if (
		(rv = pkcs11->C_GetTokenInfo (
			slotSlot,
			&info
		)) != CKR_OK
	) {
		throw CEPKCS11 ("C_GetTokenInfo", rv);
	}

	printf (
		(
			"Token Information:\n"
			"%30s: %s\n"
			"%30s: %s\n"
			"%30s: %s\n"
			"%30s: %s\n"
			"%30s: %s\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %ld\n"
			"%30s: %03d.%03d\n"
			"%30s: %03d.%03d\n"
			"%30s: %s\n"
		),
		"label", FixedStringToString (info.label, sizeof (info.label)).c_str (),
		"manufacturerID", FixedStringToString (info.manufacturerID, sizeof (info.manufacturerID)).c_str (),
		"model", FixedStringToString (info.model, sizeof (info.model)).c_str (),
		"serialNumber", FixedStringToString (info.serialNumber, sizeof (info.serialNumber)).c_str (),
		"flags", Resolve_CK_FLAGS (info.flags).c_str (),
		"ulMaxSessionCount", info.ulMaxSessionCount,
		"ulMaxSessionCount", info.ulMaxSessionCount,
		"ulMaxPinLen", info.ulMaxPinLen,
		"ulMinPinLen", info.ulMinPinLen,
		"ulTotalPublicMemory", info.ulTotalPublicMemory,
		"ulFreePublicMemory", info.ulFreePublicMemory,
		"ulTotalPrivateMemory", info.ulTotalPrivateMemory,
		"ulFreePrivateMemory", info.ulFreePrivateMemory,
		"hardwareVersion", info.hardwareVersion.major, info.hardwareVersion.minor,
		"firmwareVersion", info.firmwareVersion.major, info.firmwareVersion.minor,
		"utcTime", FixedStringToString (info.utcTime, sizeof (info.utcTime)).c_str ()
	);

	if (
		(rv=pkcs11->C_Login (
			hSession,
			CKU_USER,
			(CK_CHAR_PTR)strPIN.c_str (),
			(CK_ULONG)strPIN.length ()
		)) != CKR_OK &&
		rv != CKR_USER_ALREADY_LOGGED_IN
	) {
		throw CEPKCS11 ("C_Login", rv);
	}

	if (
		(rv=pkcs11->C_FindObjectsInit (
			hSession,
			NULL_PTR,
			0
		)) != CKR_OK
	) {
		throw CEPKCS11 ("C_FindObjectsInit", rv);
	}

	if (
		(rv=pkcs11->C_FindObjects (
			hSession,
			objects,
			objectsmax,
			&objectssize
		)) != CKR_OK
	) {
		throw CEPKCS11 ("C_FindObjects", rv);
	}

	for (i=0;i<objectssize;i++) {

		printf ("Object %lu\n", i);

		CK_ULONG lObjectSize;
		if (
			(rv=pkcs11->C_GetObjectSize (
				hSession,
				objects[i],
				&lObjectSize
			)) != CKR_OK
		) {
			printf ("%30s: Unknown\n", "Object size");
		}
		else {
			printf ("%30s: %ld\n", "Object size", lObjectSize);
		}

		for (int j=0;attrdescAttributes[j].nId!=-1;j++) {
			char Buffer[10*1024];
			CK_ATTRIBUTE t[] = {
				{(CK_ATTRIBUTE_TYPE)attrdescAttributes[j].nId, Buffer, sizeof (Buffer)}
			};

			if (
				(rv=pkcs11->C_GetAttributeValue (
					hSession,
					objects[i],
					t,
					sizeof (t) / sizeof (CK_ATTRIBUTE)
				)) == CKR_OK
			) {
				printf ("%30s: ", attrdescAttributes[j].szName);

				switch (attrdescAttributes[j].attrtypeType) {
					case attrtypeUnknown:
						printf ("Unknown value type");
					break;
					case attrtypeString:
					case attrtypeCK_CHAR:
					case attrtypeCK_DATE:
						printf ("%s", FixedStringToString (t[0].pValue, t[0].ulValueLen).c_str ());
					break;
					case attrtypeBigInteger:
					case attrtypeByteArray:
						printf ("%s", DumpToString (t[0].pValue, t[0].ulValueLen).c_str ());
					break;
					break;
					case attrtypeSubject:
					case attrtypeSubject1:
						printf ("%s\n", ParseSubject (t[0].pValue, t[0].ulValueLen).c_str ());
					break;
					case attrtypeCK_BBOOL:
						{
							CK_BBOOL b = *(CK_BBOOL *)t[0].pValue;
							if (b != CK_FALSE) {
								printf ("TRUE");
							}
							else {
								printf ("FALSE");
							}
						}
					break;
					case attrtypeCK_ULONG:
						{
							CK_ULONG l = *(CK_ULONG *)t[0].pValue;
							printf ("%ld", l);
						}
					break;
					case attrtypeCK_CERTIFICATE_TYPE:
						printf ("%s", Resolve_CK_CERTIFICATE_TYPE (*(CK_CERTIFICATE_TYPE *)t[0].pValue).c_str ());
					break;
					case attrtypeCK_KEY_TYPE:
						printf ("%s", Resolve_CK_KEY_TYPE (*(CK_KEY_TYPE *)t[0].pValue).c_str ());
					break;
					case attrtypeCK_MECHANISM_TYPE:
						printf ("%s", Resolve_CK_MECHANISM_TYPE (*(CK_MECHANISM_TYPE *)t[0].pValue).c_str ());
					break;
					case attrtypeCK_OBJECT_CLASS:
						printf ("%s", Resolve_CK_OBJECT_CLASS (*(CK_OBJECT_CLASS *)t[0].pValue).c_str ());
					break;
					case attrtypeCK_HW_FEATURE_TYPE:
						printf ("%s", Resolve_CK_HW_FEATURE_TYPE (*(CK_HW_FEATURE_TYPE *)t[0].pValue).c_str ());
					break;
					case attrtypeCK_ATTRIBUTE_PTR:
						printf ("Exists but value not parsed");
					break;
					case attrtypeCK_MECHANISM_TYPE_PTR:
						printf ("Exists but value not parsed");
					break;
					default:
						printf ("Unknown type");
					break;
				}

				printf ("\n");
			}
		}
	}

	pkcs11->C_FindObjectsFinal (hSession);
	pkcs11->C_Logout (hSession);
	pkcs11->C_CloseSession (hSession);
}