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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
/* ****************************************************************************
* 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 "Card.h"
#include "../common/Log.h"
namespace eIDMW
{
CCard::CCard(unsigned long hCard, CContext *poContext, CPinpad *poPinpad) :
m_hCard(hCard), m_poContext(poContext), m_poPinpad(poPinpad),
m_oCache(poContext), m_ulLockCount(0), m_bSerialNrString(false)
{
}
CCard::~CCard(void)
{
Disconnect(DISCONNECT_LEAVE_CARD);
}
void CCard::Disconnect(tDisconnectMode disconnectMode)
{
if (m_hCard != 0)
{
unsigned long hTemp = m_hCard;
m_hCard = 0;
m_poContext->m_oPCSC.Disconnect(hTemp, disconnectMode);
}
}
CByteArray CCard::GetATR()
{
return m_poContext->m_oPCSC.GetATR(m_hCard);
}
bool CCard::Status()
{
return m_poContext->m_oPCSC.Status(m_hCard);
}
bool CCard::IsPinpadReader()
{
return false;
}
std::string CCard::GetPinpadPrefix()
{
return "";
}
std::string CCard::GetSerialNr()
{
if (!m_bSerialNrString)
{
m_csSerialNr = GetSerialNrBytes().ToString(false, true);
m_bSerialNrString = true;
}
return m_csSerialNr;
}
CByteArray CCard::GetSerialNrBytes()
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
std::string CCard::GetLabel()
{
return "";
}
CByteArray CCard::GetInfo()
{
return CByteArray();
}
void CCard::Lock()
{
if (m_ulLockCount == 0)
m_poContext->m_oPCSC.BeginTransaction(m_hCard);
m_ulLockCount++;
}
void CCard::Unlock()
{
if (m_ulLockCount == 0)
MWLOG(LEV_ERROR, MOD_CAL, L"More Unlock()s then Lock()s called!!");
else
{
m_ulLockCount--;
if (m_ulLockCount == 0)
m_poContext->m_oPCSC.EndTransaction(m_hCard);
}
}
void CCard::SelectApplication(const CByteArray & oAID)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
CByteArray CCard::ReadCachedFile(const std::string & csPath, std::string & csName,
bool & bFound, unsigned long ulOffset, unsigned long ulMaxLen, bool &bFromDisk)
{
bFromDisk = true;
return m_oCache.GetFile(csName,
bFound, bFromDisk, ulOffset, ulMaxLen);
}
bool CCard::SerialNrPresent(const CByteArray & oData)
{
CByteArray oSerial = GetSerialNrBytes();
const unsigned char *pucSerial = oSerial.GetBytes();
unsigned long ulSerialLen = (unsigned long) oSerial.Size();
const unsigned char *pucData = oData.GetBytes();
unsigned long ulEnd = oData.Size() - ulSerialLen;
//printf("serial: %s\n", CByteArray(csSerial).ToString(false, false).c_str());
//printf("data: %s\n", oData.ToString(false, false).c_str());
for (unsigned long i = 0; i < ulEnd; i++)
{
if (memcmp(pucData + i, pucSerial, ulSerialLen) == 0)
return true;
}
return false;
}
/** Little helper function for ReadFile() */
static CByteArray ReturnData(CByteArray oData, unsigned long ulOffset, unsigned long ulMaxLen)
{
if (ulOffset == 0 && ulMaxLen == FULL_FILE)
return oData;
else
{
if (ulOffset > oData.Size())
throw CMWEXCEPTION(EIDMW_ERR_PARAM_RANGE);
if (ulMaxLen > ulOffset + oData.Size())
ulMaxLen = oData.Size() - ulOffset;
return CByteArray(oData.GetBytes() + ulOffset, ulMaxLen);
}
}
CByteArray CCard::ReadFile(const std::string & csPath,
unsigned long ulOffset, unsigned long ulMaxLen,bool bDoNotCache)
{
tCacheInfo cacheInfo = GetCacheInfo(csPath);
if (cacheInfo.action == SIMPLE_CACHE || cacheInfo.action == CHECK_SERIAL)
{
// Either read from cache or read from the card and store to cache.
// We could optimise here if part of the file has already been
// cached but not enough: in this case we can read the cached part
// and the rest from the card and put both together (and then store
// this to cache and return it).
bool bFound;
bool bFromDisk;
std::string csName = m_oCache.GetSimpleName(GetSerialNr(), csPath);
CByteArray oData = ReadCachedFile(csPath, csName, bFound,
cacheInfo.action == CHECK_SERIAL ? 0 : ulOffset,
cacheInfo.action == CHECK_SERIAL ? FULL_FILE : ulMaxLen,
bFromDisk);
if (bFound)
{
if (cacheInfo.action == CHECK_SERIAL && !SerialNrPresent(oData))
throw CMWEXCEPTION(EIDMW_CACHE_TAMPERED);
MWLOG(LEV_INFO, MOD_CAL, L" Read file %ls (%d bytes) from cache",
utilStringWiden(csPath).c_str(), oData.Size());
if (cacheInfo.action == CHECK_SERIAL)
return ReturnData(oData, ulOffset, ulMaxLen);
else
return oData;
}
else
{
oData = ReadUncachedFile(csPath, ulOffset, ulMaxLen);
if(!bDoNotCache)
{
m_oCache.StoreFile(csName, oData, ulOffset == 0 && ulMaxLen == FULL_FILE);
MWLOG(LEV_INFO, MOD_CAL, L" Stored file %ls to cache",
utilStringWiden(csPath).c_str());
}
return oData;
}
}
else if (cacheInfo.action == CHECK_16_CACHE || cacheInfo.action == CERT_CACHE)
{
// 1. In case of CHECK_16_CACHE:
// Compare 16 bytes starting at cacheInfo.ulOffset. If they are the
// same then return the cached data.
// 2. In case of CERT_CACHE:
// Read the last 16 bytes of the cert from the card, and compare
// with the cached cert. If they are the same then return the cached
// data because at end of the cert is its signature, and if something
// changed in the cert then the signature will be completely different.
// NOTE: this is only done if read from the hard disk; if the data has been
// read from the hard disk before (and is already stored in memory) then
// we won't do the above checks again.
bool bFound;
bool bFromDisk;
unsigned long ulCheckOffset = 0xFFFFFFFF;
std::string csName = m_oCache.GetSimpleName(GetSerialNr(), csPath);
CByteArray oData = ReadCachedFile(csPath, csName, bFound,
0, FULL_FILE, bFromDisk);
if (bFound && !bFromDisk)
{
MWLOG(LEV_INFO, MOD_CAL, L" Read file %ls (%d bytes) from memory cache",
utilStringWiden(csPath).c_str(), oData.Size());
return ReturnData(oData, ulOffset, ulMaxLen);
}
if (cacheInfo.action == CHECK_16_CACHE)
ulCheckOffset = cacheInfo.ulOffset;
else
{
// Our certs all start with 30 82 l1 l2 with [l1 l2] = the length of what follows
if (oData.Size() > 4 && oData.GetByte(0) == 0x30 && oData.GetByte(1) == 0x82)
{
unsigned long ulCertLen = 256 * oData.GetByte(2) + oData.GetByte(3) + 4;
if (ulCertLen > 16)
ulCheckOffset = ulCertLen - 16;
}
}
if (ulCheckOffset != 0xFFFFFFFF)
{
CByteArray oCertEnd = ReadUncachedFile(csPath, ulCheckOffset, 16);
if (oCertEnd.Size() == 16 &&
memcmp(oCertEnd.GetBytes(), oData.GetBytes() + ulCheckOffset, 16) == 0)
{
MWLOG(LEV_INFO, MOD_CAL, L" Read file %ls (%d bytes) from disk cache",
utilStringWiden(csPath).c_str(), oData.Size());
m_oCache.StoreFileToMem(csName, oData, true);
return ReturnData(oData, ulOffset, ulMaxLen);
}
else
{
MWLOG(LEV_INFO, MOD_CAL, L" Cached file %ls differs from the "
L"one on card, re-reading from card!", utilStringWiden(csPath).c_str());
}
}
oData = ReadUncachedFile(csPath, ulOffset, ulMaxLen);
if(!bDoNotCache)
{
m_oCache.StoreFile(csName, oData, ulMaxLen == FULL_FILE);
MWLOG(LEV_INFO, MOD_CAL, L" (Re)stored file %ls to cache",
utilStringWiden(csPath).c_str());
}
return oData;
}
return ReadUncachedFile(csPath, ulOffset, ulMaxLen);
}
void CCard::WriteFile(const std::string &csPath, unsigned long ulOffset,
const CByteArray & oData)
{
WriteUncachedFile(csPath, ulOffset, oData);
// We could overwrite the cache with the new data, but because
// we don't know if it's the full file, we just clear the cached
// data. This will cause a new ReadFile() to read again from the
// card but as this probably won't happen that much...
tCacheInfo cacheInfo = GetCacheInfo(csPath);
if (cacheInfo.action == SIMPLE_CACHE)
m_oCache.Delete(m_oCache.GetSimpleName(GetSerialNr(), csPath));
}
void CCard::WriteUncachedFile(const std::string &csPath, unsigned long ulOffset,
const CByteArray & oData)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
tCacheInfo CCard::GetCacheInfo(const std::string &csPath)
{
// By default no caching, card must implement this method
// to allow certain files to be cached (in a certain way).
tCacheInfo dontCache = {DONT_CACHE};
return dontCache;
}
unsigned long CCard::PinStatus(const tPin & Pin)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
bool CCard::PinCmd(tPinOperation operation, const tPin & Pin,
const std::string & csPin1, const std::string & csPin2,
unsigned long & ulRemaining, const tPrivKey *pKey)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
DlgPinUsage CCard::PinUsage2Dlg(const tPin & Pin, const tPrivKey *pKey)
{
return DLG_PIN_UNKNOWN;
}
unsigned long CCard::GetSupportedAlgorithms()
{
return 0;
}
CByteArray CCard::Sign(const tPrivKey & key, const tPin & Pin,
unsigned long algo, const CByteArray & oData)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
CByteArray CCard::Sign(const tPrivKey & key, const tPin & Pin,
unsigned long algo, CHash & oHash)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
CByteArray CCard::Decrypt(const tPrivKey & key, unsigned long algo,
const CByteArray & oData)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
CByteArray CCard::GetRandom(unsigned long ulLen)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
CByteArray CCard::SendAPDU(const CByteArray & oCmdAPDU)
{
CAutoLock oAutoLock(this);
CByteArray oResp = m_poContext->m_oPCSC.Transmit(m_hCard, oCmdAPDU);
if (oResp.Size() == 2)
{
// If SW1 = 0x61, then SW2 indicates the maximum value to be given to the
// short Le field (length of extra/ data still available) in a GET RESPONSE.
if (oResp.GetByte(0) == 0x61)
return SendAPDU(0xC0, 0x00, 0x00, oResp.GetByte(1)); // Get Response
// If SW1 = 0x6c, then SW2 indicates the value to be given to the short
// Le field (exact length of requested data) when re-issuing the same command.
if (oResp.GetByte(0) == 0x6c)
{
unsigned long ulCmdLen = oCmdAPDU.Size();
const unsigned char *pucCmd = oCmdAPDU.GetBytes();
CByteArray oNewCmdAPDU(ulCmdLen);
oNewCmdAPDU.Append(pucCmd, 4);
oNewCmdAPDU.Append(oResp.GetByte(1));
if (ulCmdLen > 5)
oNewCmdAPDU.Append(pucCmd + 5, ulCmdLen - 5);
// for cards that may need a delay (e.g. Belpic V1)
unsigned long ulDelay = Get6CDelay();
if (ulDelay != 0)
CThread::SleepMillisecs(ulDelay);
return SendAPDU(oNewCmdAPDU);
}
}
return oResp;
}
CByteArray CCard::SendAPDU(unsigned char ucINS, unsigned char ucP1, unsigned char ucP2,
unsigned long ulOutLen)
{
CByteArray oAPDU(5);
oAPDU.Append(m_ucCLA);
oAPDU.Append(ucINS);
oAPDU.Append(ucP1);
oAPDU.Append(ucP2);
oAPDU.Append((unsigned char) ulOutLen);
return SendAPDU(oAPDU);
}
CByteArray CCard::SendAPDU(unsigned char ucINS, unsigned char ucP1, unsigned char ucP2,
const CByteArray & oData)
{
CByteArray oAPDU(5 + oData.Size());
oAPDU.Append(m_ucCLA);
oAPDU.Append(ucINS);
oAPDU.Append(ucP1);
oAPDU.Append(ucP2);
oAPDU.Append((unsigned char) oData.Size());
oAPDU.Append(oData);
return SendAPDU(oAPDU);
}
CByteArray CCard::Ctrl(long ctrl, const CByteArray & oCmdData)
{
throw CMWEXCEPTION(EIDMW_ERR_NOT_SUPPORTED);
}
CP15Correction* CCard::GetP15Correction()
{
return NULL;
}
unsigned long CCard::Get6CDelay()
{
return 0;
}
unsigned char CCard::Hex2Byte(char cHex)
{
if (cHex >= '0' && cHex <= '9')
return (unsigned char) (cHex - '0');
if (cHex >= 'A' && cHex <= 'F')
return (unsigned char) (cHex - 'A' + 10);
if (cHex >= 'a' && cHex <= 'f')
return (unsigned char) (cHex - 'a' + 10);
MWLOG(LEV_ERROR, MOD_CAL, L"Invalid hex character 0x%02X found", cHex);
throw CMWEXCEPTION(EIDMW_ERR_BAD_PATH);
}
unsigned char CCard::Hex2Byte(const std::string & csHex, unsigned long ulIdx)
{
return (unsigned char) (16 * Hex2Byte(csHex[2 * ulIdx]) + Hex2Byte(csHex[2 * ulIdx + 1]));
}
bool CCard::IsDigit(char c)
{
return (c >= '0' && c <= '9');
}
unsigned long CCard::getSW12(const CByteArray & oRespAPDU, unsigned long ulExpected)
{
unsigned long ulRespLen = oRespAPDU.Size();
if (ulRespLen < 2)
{
MWLOG(LEV_ERROR, MOD_CAL, L"Only %d byte(s) returned from the card", ulRespLen);
throw CMWEXCEPTION(EIDMW_ERR_CARD_COMM);
}
unsigned long ulSW12 = 256 * oRespAPDU.GetByte(ulRespLen - 2) + oRespAPDU.GetByte(ulRespLen - 1);
if (ulExpected != 0 && ulExpected != ulSW12)
{
MWLOG(LEV_WARN, MOD_CAL, L"Card returned SW12 = %04X, expected %04X", ulSW12, ulExpected);
throw CMWEXCEPTION(m_poContext->m_oPCSC.SW12ToErr(ulSW12));
}
return ulSW12;
}
////////////////////////////////////////////////////////////////:
CAutoLock::CAutoLock(CCard *poCard) : m_poCard(poCard), m_poPCSC(NULL), m_hCard(0)
{
m_poCard->Lock();
}
CAutoLock::CAutoLock(CPCSC *poPCSC, unsigned long hCard) : m_poCard(NULL), m_poPCSC(poPCSC), m_hCard(hCard)
{
poPCSC->BeginTransaction(hCard);
}
CAutoLock::~CAutoLock()
{
if (m_poCard)
m_poCard->Unlock();
else
m_poPCSC->EndTransaction(m_hCard);
}
}
|