1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/* ****************************************************************************
* eID Middleware Project.
* Copyright (C) 2008-2009 FedICT.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version
* 3.0 as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, see
* http://www.gnu.org/licenses/.
**************************************************************************** */
#include "EmulationCard.h"
namespace eIDMW
{
unsigned char tucSW12WrongLength[2] = {0x6C, 'x'}; // put in 'x' the correct length
unsigned char tucSW12BadPin[2] = {0x63, 0x00};
CEmulationCard::CEmulationCard():
m_lDefaultRet(0),
m_oDefaultResponseAPDU(tucSW12FunctionNotSupported, sizeof(tucSW12FunctionNotSupported)),
m_bEnforce(false)
{
unsigned char tucDefaultATR[] =
{0x3B,0x98,0x13,0x40,0x0A,0xA5,0x03,0x01,0x01,0x01,0xAD,0x13,0x11};
m_oATR = CByteArray(tucDefaultATR, sizeof(tucDefaultATR));
}
CEmulationCard::CEmulationCard(const CEmulationCard &oCard):
m_lDefaultRet(oCard.m_lDefaultRet),
m_oDefaultResponseAPDU(oCard.m_oDefaultResponseAPDU),
m_bEnforce(oCard.m_bEnforce),
m_oATR(oCard.m_oATR)
{
}
void CEmulationCard::Reset()
{
}
void CEmulationCard::SetATR(CByteArray oATR)
{
m_oATR = oATR;
}
CByteArray CEmulationCard::GetATR()
{
return m_oATR;
}
LONG CEmulationCard::SCardTransmit(
IN LPCBYTE pbSendBuffer,
IN DWORD cbSendLength,
OUT LPBYTE pbRecvBuffer,
IN OUT LPDWORD pcbRecvLength)
{
LONG lRet;
if (TransmitSpecified(pbSendBuffer, cbSendLength,
pbRecvBuffer, pcbRecvLength, lRet))
{
return lRet;
}
else
return SCardTransmitInternal(pbSendBuffer, cbSendLength,
pbRecvBuffer, pcbRecvLength);
}
void CEmulationCard::SpecifyCommand(const CByteArray &oRequestAPDU,
LONG lRet, const CByteArray oResponseAPDU)
{
m_Commands.push_back(CCommand(oRequestAPDU, lRet, oResponseAPDU));
}
void CEmulationCard::ForceResponse(LONG lRet,
const CByteArray oResponseAPDU)
{
m_oDefaultResponseAPDU = oResponseAPDU;
m_lDefaultRet = true;
m_bEnforce = true;
}
void CEmulationCard::RemoveForcedResponse()
{
m_bEnforce = false;
}
bool CEmulationCard::TransmitSpecified(
IN LPCBYTE pbSendBuffer,
IN DWORD cbSendLength,
OUT LPBYTE pbRecvBuffer,
IN OUT LPDWORD pcbRecvLength,
LONG & lRet)
{
return false;
CByteArray *poResp = NULL;
bool bFound = true;
if (m_bEnforce)
{
poResp = &m_oDefaultResponseAPDU;
lRet = m_lDefaultRet;
}
else
{
CByteArray oReq(pbSendBuffer, cbSendLength);
int i;
for (i = (int) m_Commands.size() - 1; i >= 0; i--)
{
if (m_Commands[i].m_oReq.Equals(oReq))
{
poResp = &m_Commands[i].m_oReq;
lRet = m_Commands[i].m_lRet;
break;
}
}
if (i < 0)
bFound = false;
else
{
if (poResp->Size() > *pcbRecvLength)
lRet = SCARD_E_INSUFFICIENT_BUFFER;
else
{
*pcbRecvLength = m_oDefaultResponseAPDU.Size();
memcpy(pbRecvBuffer, m_oDefaultResponseAPDU.GetBytes(), *pcbRecvLength);
lRet = SCARD_S_SUCCESS;
}
}
}
return bFound;
}
// To be overwritten by the subclasses
LONG CEmulationCard::SCardTransmitInternal(
IN LPCBYTE pbSendBuffer,
IN DWORD cbSendLength,
OUT LPBYTE pbRecvBuffer,
IN OUT LPDWORD pcbRecvLength)
{
return SCARD_E_UNEXPECTED;
}
CEmulationCard CEmulationCard::operator = (const CEmulationCard &oCard)
{
if (&oCard != this)
{
this->m_lDefaultRet = oCard.m_lDefaultRet;
this->m_oDefaultResponseAPDU = oCard.m_oDefaultResponseAPDU;
this->m_bEnforce = oCard.m_bEnforce;
this->m_oATR = oCard.m_oATR;
}
return *this;
}
}
|