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
|
/* ****************************************************************************
* 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 "EmulationBeidCard.h"
#include "../common/Hash.h"
using namespace eIDMW;
CEmulationBeidCard::CEmulationBeidCard(unsigned char ucVersion)
{
m_ucVersion = ucVersion;
m_bKeySelected = false;
}
CByteArray CEmulationBeidCard::GetCardData(CByteArray oAPDU)
{
unsigned char ucCLA = oAPDU.GetByte(0);
unsigned char ucP1 = oAPDU.GetByte(2);
unsigned char ucP2 = oAPDU.GetByte(3);
unsigned char ucP3 = oAPDU.GetByte(4);
if (ucCLA != 0x80)
return CByteArray(tucSW12BadClass, sizeof(tucSW12BadClass));
if (ucP2 != 0x00)
return CByteArray(tucSW12IncorrectParamsP1P2, sizeof(tucSW12IncorrectParamsP1P2));
if (ucP1 == 0x00 || ucP1 == 0x02)
{
if ((ucP1 == 0x00 && ucP3 != 0x1C) || (ucP1 == 0x02 && ucP3 != 0x9C))
return CByteArray(tucSW12IncorrectParamsP1P2, sizeof(tucSW12IncorrectParamsP1P2));
// Signed card data not for V1 cards
if (ucP1 == 0x02 && m_ucVersion < 0x20)
return CByteArray(tucSW12InsNotFound, sizeof(tucSW12InsNotFound));
unsigned char tucDataV11[] = {
0xA5,0x03,0x01,0x01,0x01,0x11,0x00,0x02,0x00,0x01,0x01,0x0F};
unsigned char tucDataV20[] = {
0xD0,0x00,0x48,0x01,0x01,0x20,0x00,0x03,0x01,0x0F,0x02,0x8A};
CByteArray oResp(0x9C + 2);
oResp.Append(GetSerialNr());
// Add the rest, this depends on the version
if (m_ucVersion < 0x20)
oResp.Append(tucDataV11, sizeof(tucDataV11));
else
oResp.Append(tucDataV20, sizeof(tucDataV20));
if (ucP1 == 0x02)
oResp.Append(MakeFakeSignature(oResp));
oResp.Append(tucSW12OK, sizeof(tucSW12OK));
return oResp;
}
else
return CByteArray(tucSW12IncorrectParamsP1P2, sizeof(tucSW12IncorrectParamsP1P2));
}
/* For signature that can't/won't be checked */
CByteArray CEmulationBeidCard::MakeFakeSignature(const CByteArray &oData)
{
CByteArray oFakeSignature(128);
CHash oHash;
oFakeSignature.Append(GetSerialNr()); // to diversify for each card
oFakeSignature.Append(oHash.Hash(ALGO_MD5, oData));
for (int i = 0; i < 6; i++)
oFakeSignature.Append(oHash.Hash(ALGO_MD5, oFakeSignature));
return oFakeSignature;
}
/**Serial Nr = first 16 bytes, get them from the EF(TokenInfo) */
CByteArray CEmulationBeidCard::GetSerialNr()
{
CByteArray oSerial(16);
tCardFile & cardFile = m_files["3F00DF005032"];
if (cardFile.data.Size() > 23)
oSerial.Append(cardFile.data.GetBytes() + 7, 16);
else
{
// In case the EF(TokenInfo) wouldn't exist (which is an error)
unsigned char tucZeros[16] = {0x00};
oSerial.Append(tucZeros, sizeof(tucZeros));
}
return oSerial;
}
CByteArray CEmulationBeidCard::ChangePin(const CByteArray & oAPDU)
{
unsigned char ucCLA = oAPDU.GetByte(0);
unsigned char ucP1 = oAPDU.GetByte(2);
unsigned char ucP2 = oAPDU.GetByte(3);
unsigned char ucP3 = oAPDU.GetByte(4);
if (ucCLA != 0x00)
return CByteArray(tucSW12BadClass, sizeof(tucSW12BadClass));
if (ucP1 == 0x01)
return CByteArray(tucSW12FunctionNotSupported, sizeof(tucSW12FunctionNotSupported));
if (ucP1 != 0x00)
return CByteArray(tucSW12IncorrectParamsP1P2, sizeof(tucSW12IncorrectParamsP1P2));
if (ucP3 != 0x10)
return CByteArray(tucSW12WrongLengthP3, sizeof(tucSW12WrongLengthP3));
tCardPin *poCardPin = FindPin(ucP2);
if (poCardPin == NULL)
return CByteArray(tucSW12RefDataNotFound, sizeof(tucSW12RefDataNotFound));
if (poCardPin->ulPinTries == 0)
return CByteArray(tucSW12PinBlocked, sizeof(tucSW12PinBlocked));
char csNewPin[13] = {0};
int iCount = oAPDU.GetByte(13) & 0x0F;
if (iCount < 4 || iCount > 12 || oAPDU.GetByte(20) != 0xFF)
return CByteArray(tucSW12WrongData, sizeof(tucSW12WrongData));
iCount = 0;
for (int i = 14; i < 20; i++)
{
unsigned char uc = oAPDU.GetByte(i);
if ((uc & 0xF0) == 0xF0)
break;
csNewPin[iCount++] = 0x30 + ((uc & 0xF0) / 16);
if ((uc & 0x0F) == 0x0F)
break;
csNewPin[iCount++] = 0x30 + (uc & 0x0F);
}
poCardPin->ulPinTries--;
CByteArray oCardPinBuf = MakePinBuf(poCardPin->csPin, *poCardPin);
CByteArray oPresentedPinBuf(oAPDU.GetBytes() + 5, 8);
if (!oCardPinBuf.Equals(oPresentedPinBuf))
{
tucSW12BadPin[1] = (unsigned char) (0xC0 + poCardPin->ulPinTries);
return CByteArray(tucSW12BadPin, sizeof(tucSW12BadPin));
}
poCardPin->ulPinTries = poCardPin->ulMaxPinTries;
poCardPin->bVerifyOK = true;
poCardPin->csPin = csNewPin;
return CByteArray(tucSW12OK, sizeof(tucSW12OK));
}
CByteArray CEmulationBeidCard::SetSecurityEnv(const CByteArray & oAPDU)
{
unsigned char ucCLA = oAPDU.GetByte(0);
unsigned char ucP1 = oAPDU.GetByte(2);
unsigned char ucP2 = oAPDU.GetByte(3);
unsigned char ucP3 = oAPDU.GetByte(4);
m_bKeySelected = false;
if (ucCLA != 0x00)
return CByteArray(tucSW12BadClass, sizeof(tucSW12BadClass));
if (ucP1 != 0x41 || ucP2 != 0xB6)
return CByteArray(tucSW12IncorrectParamsP1P2, sizeof(tucSW12IncorrectParamsP1P2));
if (ucP3 != 0x05)
return CByteArray(tucSW12WrongLengthP3, sizeof(tucSW12WrongLengthP3));
if (oAPDU.GetByte(5) != 0x04 || oAPDU.GetByte(6) != 0x80 || oAPDU.GetByte(8) != 0x84)
return CByteArray(tucSW12WrongData, sizeof(tucSW12WrongData));
m_ucSelectedAlgo = oAPDU.GetByte(7);
unsigned char ulKeyRef = oAPDU.GetByte(9);
tCardKey *key = FindKey(ulKeyRef);
if (key == NULL)
return CByteArray(tucSW12RefDataNotFound, sizeof(tucSW12RefDataNotFound));
if (m_ucSelectedAlgo == 0x01)
m_ucSelectedAlgo = ALGO_RSA_PKCS1;
else if (m_ucSelectedAlgo == 0x02)
m_ucSelectedAlgo = ALGO_RSA_PKCS1_SHA1;
else if (m_ucSelectedAlgo == 0x04)
m_ucSelectedAlgo = ALGO_RSA_PKCS1_MD5;
else if (m_ucVersion >= 0x20 && m_ucSelectedAlgo == 0x10)
m_ucSelectedAlgo = ALGO_RSA_PSS_SHA1;
else
return CByteArray(tucSW12WrongData, sizeof(tucSW12WrongData));
if (!(m_ucSelectedAlgo & key->ulAlgos))
return CByteArray(tucSW12WrongData, sizeof(tucSW12WrongData));
m_ucSelectedKeyRef = ulKeyRef;
m_bKeySelected = true;
return CByteArray(tucSW12OK, sizeof(tucSW12OK));
}
CByteArray CEmulationBeidCard::PSOSign(const CByteArray & oAPDU)
{
unsigned char ucCLA = oAPDU.GetByte(0);
unsigned char ucP1 = oAPDU.GetByte(2);
unsigned char ucP2 = oAPDU.GetByte(3);
unsigned char ucP3 = oAPDU.GetByte(4);
if (ucCLA != 0x00)
return CByteArray(tucSW12BadClass, sizeof(tucSW12BadClass));
if (ucP1 != 0x9E || ucP2 != 0x9A)
return CByteArray(tucSW12IncorrectParamsP1P2, sizeof(tucSW12IncorrectParamsP1P2));
if (!m_bKeySelected)
return CByteArray(tucSW12ConditionsOfUseNOK, sizeof(tucSW12ConditionsOfUseNOK));
tCardKey *poKey = FindKey(m_ucSelectedKeyRef);
if (poKey == NULL)
return CByteArray(tucSW12RefDataNotFound, sizeof(tucSW12RefDataNotFound));
tCardPin *pin = FindPin(poKey->ulPinRef);
if (pin != NULL)
{
if (!pin->bVerifyOK)
return CByteArray(tucSW12SecStatusNotSatisfied, sizeof(tucSW12SecStatusNotSatisfied));
if (poKey->bUserConsent && !m_bPreviousCommandWasNonRepVerify)
return CByteArray(tucSW12SecStatusNotSatisfied, sizeof(tucSW12SecStatusNotSatisfied));
}
return Sign(CByteArray(oAPDU.GetBytes() + 5, oAPDU.Size() - 5),
poKey, m_ucSelectedAlgo);
}
CByteArray CEmulationBeidCard::GetPinStatus(const CByteArray & oAPDU)
{
unsigned char ucCLA = oAPDU.GetByte(0);
unsigned char ucP1 = oAPDU.GetByte(2);
unsigned char ucP2 = oAPDU.GetByte(3);
unsigned char ucP3 = oAPDU.GetByte(4);
// Not for V1 cards
if (m_ucVersion < 0x20)
return CByteArray(tucSW12InsNotFound, sizeof(tucSW12InsNotFound));
if (ucCLA != 0x80)
return CByteArray(tucSW12BadClass, sizeof(tucSW12BadClass));
if (ucP1 != 0x00 && ucP1 != 0x02)
return CByteArray(tucSW12FunctionNotSupported, sizeof(tucSW12FunctionNotSupported));
if ((ucP1 == 0x00 && ucP3 != 0x01) || (ucP1 == 0x02 && ucP3 != 0x81))
return CByteArray(tucSW12WrongLengthP3, sizeof(tucSW12WrongLengthP3));
tCardPin *poCardPin = FindPin(ucP2);
if (poCardPin == NULL)
return CByteArray(tucSW12RefDataNotFound, sizeof(tucSW12RefDataNotFound));
CByteArray oResp(0x81 + 2);
oResp.Append((unsigned char) poCardPin->ulPinTries);
if (ucP1 == 0x02)
oResp.Append(MakeFakeSignature(oResp));
oResp.Append(tucSW12OK, sizeof(tucSW12OK));
return oResp;
}
CByteArray CEmulationBeidCard::InternalAuth(const CByteArray & oAPDU)
{
unsigned char ucCLA = oAPDU.GetByte(0);
unsigned char ucP1 = oAPDU.GetByte(2);
unsigned char ucP2 = oAPDU.GetByte(3);
unsigned char ucP3 = oAPDU.GetByte(4);
if (ucCLA != 0x00)
return CByteArray(tucSW12BadClass, sizeof(tucSW12BadClass));
if (ucP1 != 0x02)
return CByteArray(tucSW12WrongParams, sizeof(tucSW12WrongParams));
if (ucP2 != 0x81)
return CByteArray(tucSW12RefDataNotFound, sizeof(tucSW12RefDataNotFound));
if ((ucP3 != 0x16))
return CByteArray(tucSW12WrongLengthP3, sizeof(tucSW12WrongLengthP3));
CByteArray oResp = MakeFakeSignature(CByteArray(oAPDU.GetBytes() + 7, 20));
oResp.Append(tucSW12OK, sizeof(tucSW12OK));
return oResp;
}
LONG CEmulationBeidCard::SCardTransmitInternal(
IN LPCBYTE pbSendBuffer,
IN DWORD cbSendLength,
OUT LPBYTE pbRecvBuffer,
IN OUT LPDWORD pcbRecvLength)
{
unsigned char ucINS = pbSendBuffer[1];
CByteArray oReqAPDU(pbSendBuffer, cbSendLength);
CByteArray oRespAPDU;
if (ucINS == 0x2A)
{
oRespAPDU = PSOSign(oReqAPDU);
m_bPreviousCommandWasNonRepVerify = false;
}
else
{
m_bPreviousCommandWasNonRepVerify = false;
switch(ucINS)
{
case 0xE4:
oRespAPDU = GetCardData(oReqAPDU);
break;
case 0xA4:
// A little special to the Belpic applet: select 3F00 always means
// selecting the root..
if (pbSendBuffer[2] == 0x02 && pbSendBuffer[4] == 0x02 &&
pbSendBuffer[5] == 0x3F && pbSendBuffer[6] == 0x00)
{
m_csCurrentDF = "3F00";
m_csCurrentEF = "";
oRespAPDU = CByteArray(tucSW12OK, sizeof(tucSW12OK));
break;
}
return CFileSystemCard::SCardTransmitInternal(pbSendBuffer, cbSendLength,
pbRecvBuffer, pcbRecvLength);
case 0x20:
oRespAPDU = VerifyPin(oReqAPDU);
m_bPreviousCommandWasNonRepVerify = (oRespAPDU.Equals(oSW12OK) &&
((m_ucVersion < 0x20) || oReqAPDU.GetByte(3) == 0x86));
break;
case 0x22:
oRespAPDU = SetSecurityEnv(oReqAPDU);
break;
case 0x24:
oRespAPDU = ChangePin(oReqAPDU);
break;
case 0xEA:
oRespAPDU = GetPinStatus(oReqAPDU);
break;
case 0x88:
oRespAPDU = InternalAuth(oReqAPDU);
break;
default:
return CFileSystemCard::SCardTransmitInternal(pbSendBuffer, cbSendLength,
pbRecvBuffer, pcbRecvLength);
}
}
if (*pcbRecvLength < oRespAPDU.Size())
return SCARD_E_INSUFFICIENT_BUFFER;
*pcbRecvLength = oRespAPDU.Size();
memcpy(pbRecvBuffer, oRespAPDU.GetBytes(), *pcbRecvLength);
return SCARD_S_SUCCESS;
}
|