MscTokenConnection.cpp [plain text]
#include <iostream>
#include "MscTokenConnection.h"
#include "MscError.h"
MscTokenConnection::MscTokenConnection(const MSCTokenInfo &rTokenInfo)
{
secdebug("connection", "Calling MscTokenConnection::MscTokenConnection");
clearPod();
::memcpy(&mLocalTokenInfo,&rTokenInfo,sizeof(MSCTokenInfo));
#ifdef _DEBUG_OSTREAM
std::cout << "Dump: \n" << mLocalTokenInfo << std::endl;
#endif
}
MscTokenConnection::MscTokenConnection(const MSCTokenConnection &rTokenConnection)
{
hContext = rTokenConnection.hContext; hCard = rTokenConnection.hCard; ioType->dwProtocol = rTokenConnection.ioType->dwProtocol; ioType->cbPciLength = rTokenConnection.ioType->cbPciLength; macSize = rTokenConnection.macSize; loggedIDs = rTokenConnection.loggedIDs; shareMode = rTokenConnection.shareMode;
::strncpy(reinterpret_cast<char *>(pMac), reinterpret_cast<const char *>(rTokenConnection.pMac),
min(static_cast<size_t>(rTokenConnection.macSize),sizeof(pMac))); }
MscTokenConnection &MscTokenConnection::operator = (const MSCTokenConnection &rTokenConnection)
{
hContext = rTokenConnection.hContext; hCard = rTokenConnection.hCard; ioType->dwProtocol = rTokenConnection.ioType->dwProtocol; ioType->cbPciLength = rTokenConnection.ioType->cbPciLength; macSize = rTokenConnection.macSize; loggedIDs = rTokenConnection.loggedIDs; shareMode = rTokenConnection.shareMode;
::strncpy(reinterpret_cast<char *>(pMac), reinterpret_cast<const char *>(rTokenConnection.pMac),
min(static_cast<size_t>(rTokenConnection.macSize),sizeof(pMac)));
return *this;
}
void MscTokenConnection::connect(const char *applicationName,MSCULong32 sharingMode)
{
MSC_RV rv = MSCReleaseConnection(this, MSC_RESET_TOKEN);
rv = MSCEstablishConnection(&mLocalTokenInfo, sharingMode,
reinterpret_cast<unsigned char *>(const_cast<char *>(applicationName)),
applicationName?strlen(applicationName):0, this); if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::release(MSCULong32 endAction)
{
MSC_RV rv = MSCReleaseConnection(this,endAction);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::beginTransaction()
{
MSC_RV rv = MSCBeginTransaction(this);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::endTransaction(MSCULong32 endAction)
{
MSC_RV rv = MSCEndTransaction(this,endAction);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::logoutAll()
{
MSC_RV rv = MSCLogoutAll(this);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::verifyPIN(MSCUChar8 pinNum,std::string pin)
{
MSC_RV rv = MSCVerifyPIN(this,pinNum,reinterpret_cast<unsigned char *>(const_cast<char *>(pin.c_str())),pin.length());
if (rv!=MSC_SUCCESS)
{
std::cout << "*** PIN verify failed!!! ***" << std::endl;
MscError::throwMe(rv);
}
}
unsigned int MscTokenConnection::listPins()
{
MSCUShort16 mask;
MSC_RV rv = MSCListPINs(this, &mask);
if (rv != MSC_SUCCESS)
MscError::throwMe(rv);
return mask;
}
void MscTokenConnection::selectAID(std::string aid)
{
selectAID(aid.c_str(), aid.length());
}
void MscTokenConnection::selectAID(const char *, MSCULong32 )
{
MSC_RV rv = MSC_UNSUPPORTED_FEATURE; if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::writeFramework(const MSCInitTokenParams& initParams)
{
MSC_RV rv = MSCWriteFramework(this,const_cast<MSCInitTokenParams *>(&initParams));
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::getKeyAttributes(MSCUChar8 keyNumber,MSCKeyInfo& keyInfo)
{
MSC_RV rv = MSCGetKeyAttributes(this,keyNumber,&keyInfo);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::getObjectAttributes(std::string objectID,MSCObjectInfo& objectInfo)
{
MSC_RV rv = MSCGetObjectAttributes(this,const_cast<char *>(objectID.c_str()),&objectInfo);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
void MscTokenConnection::getStatus(MSCStatusInfo& statusInfo)
{
MSC_RV rv = MSCGetStatus(this,&statusInfo);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
#pragma mark ---------------- Token state methods --------------
bool MscTokenConnection::tokenWasReset()
{
return MSCIsTokenReset(this);
}
bool MscTokenConnection::clearReset()
{
return MSCClearReset(this);
}
bool MscTokenConnection::moved()
{
return MSCIsTokenMoved(this);
}
bool MscTokenConnection::changed()
{
return MSCIsTokenChanged(this);
}
bool MscTokenConnection::known()
{
return MSCIsTokenKnown(this);
}
#pragma mark ---------------- Capability methods --------------
MSCULong32 MscTokenConnection::getCapabilities(MSCULong32 tag)
{
MSCULong32 cap;
MSCULong32 size;
MSC_RV rv = MSCGetCapabilities(this, tag,
reinterpret_cast<MSCPUChar8>(&cap), &size);
if (rv != MSC_SUCCESS)
MscError::throwMe(rv);
if (size == 1)
return *reinterpret_cast<uint8_t *>(&cap);
else if (size == 2)
return *reinterpret_cast<uint16_t *>(&cap);
else
return cap;
}
void MscTokenConnection::extendedFeature(MSCULong32 extFeature,MSCPUChar8 outData,MSCULong32 outLength,
MSCPUChar8 inData, MSCPULong32 inLength)
{
MSC_RV rv = MSCExtendedFeature(this,extFeature,outData,outLength,inData,inLength);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
#pragma mark ---------------- Key methods --------------
void MscTokenConnection::generateKeys(MSCUChar8 prvKeyNum,MSCUChar8 pubKeyNum,MSCGenKeyParams& params)
{
MSC_RV rv = MSCGenerateKeys(this,prvKeyNum,pubKeyNum,¶ms);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}
#pragma mark ---------------- Misc methods --------------
void MscTokenConnection::getChallenge(const char *seed,size_t seedSize,const char *randomData,size_t randomDataSize)
{
MSC_RV rv = MSCGetChallenge(this,reinterpret_cast<unsigned char *>(const_cast<char *>(seed)),seedSize,
reinterpret_cast<unsigned char *>(const_cast<char *>(randomData)),randomDataSize);
if (rv!=MSC_SUCCESS)
MscError::throwMe(rv);
}