Пример #1
0
// Auth1PrivateKeyBlock::PackData
// Packs member data into raw buffer in base class.  Returns true on success and
// false on failure.  Makes sure keyList is not empty.  Appends number of keys
// in key list.  For each key, appends key length and the key.
bool
Auth1PrivateKeyBlock::PackData()
{
	WTRACE("Auth1PrivateKeyBlock::PackData");
	if (! AuthPublicKeyBlockBase::PackData()) return false;

	// KeyList cannot be empty.
	WDBG_LL("Auth1PrivateKeyBlock::PackData Validating...");
	if (mKeyList.empty())
	{
		WDBG_LH("Auth1PrivateKeyBlock::PackData KeySet is empty, pack fails.");
		return false;
	}

	// Append fixed length data
	WDBG_LL("Auth1PrivateKeyBlock::PackData Packing...");
	unsigned short aNumKeys = mKeyList.size();
	unsigned short tmpNumKeys = getLittleEndian(aNumKeys);
	mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpNumKeys), sizeof(tmpNumKeys));

	// Append each key (length and binary data)
	WDBG_LL("Auth1PrivateKeyBlock::PackData Packing keys (" << mKeyList.size() << ')');
	PrivateKeyList::iterator anItr(mKeyList.begin());
	for (; anItr != mKeyList.end(); anItr++)
	{
		unsigned short aKeyLen = (*anItr)->GetKeyLen();
		unsigned short tmpKeyLen = getLittleEndian(tmpKeyLen);
		mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpKeyLen), sizeof(tmpKeyLen));
		mRawBuf.append((*anItr)->GetKey(), aKeyLen);

        // count and record which BlockId's map to this key.
	    PrivateKeyMap::iterator aMapIter(mKeyMap.begin());
		unsigned short aBlockIDCount=0;
        std::list<unsigned short> aBlockIdList;
        for(; aMapIter != mKeyMap.end(); aMapIter++)
        {
            if( aMapIter->second == *anItr )
            {
                aBlockIDCount++;
                aBlockIdList.push_back( aMapIter->first );
            }
        }

		unsigned short tmpBlockIDCount = getLittleEndian(aBlockIDCount);
		
        // pack BlockId count and BlockIds
		mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpBlockIDCount), sizeof(tmpBlockIDCount));
        while( aBlockIdList.size() )
        {
            unsigned short aBlockId = aBlockIdList.front();
            makeLittleEndian(aBlockId);
		    mRawBuf.append(reinterpret_cast<unsigned char*>(& aBlockId), sizeof(unsigned short));
            aBlockIdList.pop_front();
        }
	}

	return true;
}
// ClientCDKey::EncryptKey
// Encrypts into 8 byte buffer in 16 byte binary.  Uses symmetric key built from product
// name for encryption.
bool
ClientCDKey::EncryptKey(const __int64& theBufR) const
{
	WTRACE("ClientCDKey::EncryptKey");
	try
	{
		// Build symmetric key from product
		WDBG_LL("ClientCDKey::EncryptKey Creating symmetric key from product=" << mProduct);
		BFSymmetricKey aSymKey;
		CreateSymmetricKey(aSymKey);

		// Decrypt the key
		WDBG_LL("ClientCDKey::EncryptKey Encrypting CDKey.");
		__int64 tmpBuf = getLittleEndian(theBufR);			// WON 3/21/00
		BFSymmetricKey::CryptReturn anEncrypt(aSymKey.Encrypt(reinterpret_cast<const unsigned char*>(&tmpBuf), sizeof(tmpBuf)));
		auto_ptr<unsigned char>     aDelP(anEncrypt.first);
		if (anEncrypt.second != BINARYKEY_LEN)
		{
			WDBG_LM("ClientCDKey::EncryptKey Encrypt of key has bad length.");
			return false;
		}

		// Fill BinKey with encrypted key
		mBinKey.assign(anEncrypt.first, anEncrypt.second);
	}
	catch (WONCrypt::CryptException& anExR)
	{
		WDBG_LH("ClientCDKey::EncryptKey exception encrypting key: " << anExR);
		return false;
	}

	return true;
}
// EGPublicKey::EncryptData
// Encrypts a block of specified length into the specified	queue.  Determines the
// number of individual blocks to be encrypted.  Adds block count to queue.  Then
// calls EncryptBlock() to encrypt each block into the queue.  Note that mCryptP
// must be valid before this method is called.
void
EGPublicKey::EncryptData(BufferedTransformation& aQueue, const unsigned char* theMsgP,
                         unsigned long theLen) const
{
	WTRACE("EGPublicKey::EncryptData");
	WDBG_LL("EGPublicKey::EncryptData, len=" << theLen);

	// Determine block length and number of blocks
	unsigned long aBlockLen = mCryptP->MaxPlainTextLength();
	unsigned long aNumBlock = theLen / aBlockLen;
	if ((theLen % aBlockLen) != 0) aNumBlock++;
	WDBG_LL("EGPublicKey::EncryptBlock NumBlocks=" << aNumBlock << ", BlockLen=" << aBlockLen);

	// A num blocks to output
	unsigned long tmpNumBlock = getLittleEndian(aNumBlock);
	aQueue.Put(reinterpret_cast<unsigned char*>(&tmpNumBlock), sizeof(tmpNumBlock));

	// Encrypt the data, one block at a time
	while (theLen > aBlockLen)
	{
		EncryptBlock(aQueue, theMsgP, aBlockLen);
		theMsgP += aBlockLen;
		theLen  -= aBlockLen;
	}

	// Encrypt the last block and close the queue
	EncryptBlock(aQueue, theMsgP, theLen);
	aQueue.Close();
}
Пример #4
0
// Auth1PublicKeyBlock::PackData
// Packs member data into raw buffer in base class.  Returns true on success and
// false on failure.  Makes sure keyList is not empty.  Appends number of keys
// in key list.  For each key, appends key length and the key.
bool
Auth1PublicKeyBlock::PackData()
{
	WTRACE("Auth1PublicKeyBlock::PackData");
	if (! AuthPublicKeyBlockBase::PackData()) return false;

	// KeyList cannot be empty.
	WDBG_LL("Auth1PublicKeyBlock::PackData Validating...");
	if (mKeyList.empty())
	{
		WDBG_LH("Auth1PublicKeyBlock::PackData KeySet is empty, pack fails.");
		return false;
	}

	// Append fixed length data
	WDBG_LL("Auth1PublicKeyBlock::PackData Packing...");
	unsigned short aNumKeys = mKeyList.size();
	makeLittleEndian(aNumKeys);
	mRawBuf.append(reinterpret_cast<unsigned char*>(&aNumKeys), sizeof(aNumKeys));

	// Append each key (length and binary data)
	WDBG_LL("Auth1PublicKeyBlock::PackData Packing keys (" << mKeyList.size() << ')');
	PublicKeyList::iterator anItr(mKeyList.begin());
	for (; anItr != mKeyList.end(); anItr++)
	{
		unsigned short aKeyLen = anItr->GetKeyLen();
		unsigned short tmpKeyLen = getLittleEndian(aKeyLen);
		mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpKeyLen), sizeof(tmpKeyLen));
		mRawBuf.append(anItr->GetKey(), aKeyLen);
	}

	return true;
}
Пример #5
0
// Auth1Certificate::PackData
// Packs member data into raw buffer in base class.  Returns true on success and
// false on failure.  Verifies member data and appends member data to buffer.
bool
Auth1Certificate::PackData()
{
	WTRACE("Auth1Certificate::PackData");
	if (! AuthCertificateBase::PackData()) return false;

	// UserId and CommunityId must not be zero.
	WDBG_LL("Auth1Certificate::PackData Validating...");
	if ((GetUserId() == 0) || (GetCommunityId() == 0))
	{
		WDBG_LH("Auth1Certificate::PackData UserId or CommunityId are 0, pack fails.");
		return false;
	}

	// PubKey must be valid
	if (mPubKey.GetKeyLen() <= 1)
	{
		WDBG_LH("Auth1Certificate::PackData PubKey not valid, pack fails.");
		return false;
	}

	// Append fixed length data
	WDBG_LL("Auth1Certificate::PackData Packing...");
	unsigned long tmpUserId = getLittleEndian(mUserId);
	unsigned long tmpCommunityId = getLittleEndian(GetCommunityId());
	unsigned short tmpTrustLevel = getLittleEndian(GetTrustLevel());
	mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpUserId),      sizeof(tmpUserId));
	mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpCommunityId), sizeof(tmpCommunityId));
	mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpTrustLevel),  sizeof(tmpTrustLevel));

	// Append PubKey length and PubKey
	WDBG_LL("Auth1Certificate::PackData Packing PublicKey.");
	unsigned short aKeyLen = mPubKey.GetKeyLen();
	unsigned short tmpKeyLen = getLittleEndian(aKeyLen);
	mRawBuf.append(reinterpret_cast<unsigned char*>(&tmpKeyLen), sizeof(tmpKeyLen));
	mRawBuf.append(mPubKey.GetKey(), aKeyLen);

	return true;
}
// ClientCDKey::FieldsFromBuffer
// Fill in internal fields from 8 byte buffer.  Light check is byte 3, key is bytes
// 0-2 and 4-7.
void
ClientCDKey::FieldsFromBuffer(const __int64& theBuf)
{
	WTRACE("ClientCDKey::FieldsFromBuffer");
	WDBG_LL("ClientCDKey::FieldsFromBuffer Buffer=" << hex << theBuf << dec);
	__int64 tmpBuf = getLittleEndian(theBuf);									// WON 3/21/00
	const unsigned char* aP = reinterpret_cast<const unsigned char*>(&tmpBuf);

	// Extract lightCheck, byte 3
	mLightCheck = *(aP+3);
	WDBG_LL("ClientCDKey::FieldsFromBuffer lightCheck=" << hex << mLightCheck << dec);

	// Key value is rest of bytes (0-2, 4-7)
	mKey.assign(aP, 3);
	mKey.append(aP+4, 4);
	WDBG_LL("ClientCDKey::FieldsFromBuffer key=" << mKey);
}