Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1040 lines
27 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. smcutil.c
  5. Abstract:
  6. This module contains some utility fucntions
  7. Environment:
  8. Kernel mode only.
  9. Notes:
  10. Revision History:
  11. - Created December 1996 by Klaus Schutz
  12. - Nov 97: Released Version 1.0
  13. - Jan 98: Calc. of GT changed. (Now set to 0 if N = 0 or N = 255)
  14. Rules for setting card status to SCARD_SPECIFIC changed
  15. Default clock freq. is now used for initial etu calculation
  16. --*/
  17. #define _ISO_TABLES_
  18. #ifdef SMCLIB_VXD
  19. #define try
  20. #define leave goto __label
  21. #define finally __label:
  22. #else
  23. #if !defined(SMCLIB_CE)
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ntddk.h>
  28. #endif
  29. #endif
  30. #ifdef SMCLIB_TEST
  31. #define DbgPrint printf
  32. #define DbgBreakPoint()
  33. #define RtlAssert
  34. #endif
  35. #define _SMCUTIL_
  36. #include "smclib.h"
  37. #define IS_VENDOR(a) (memcmp(SmartcardExtension->VendorAttr.VendorName.Buffer, a, SmartcardExtension->VendorAttr.VendorName.Length) == 0)
  38. #define IS_IFDTYPE(a) (memcmp(SmartcardExtension->VendorAttr.IfdType.Buffer, a, SmartcardExtension->VendorAttr.IfdType.Length) == 0)
  39. void
  40. DumpData(
  41. const ULONG DebugLevel,
  42. PUCHAR Data,
  43. ULONG DataLen
  44. );
  45. //
  46. // This is the time resolution.
  47. // We calculate all times not in seconds, but in micro seconds
  48. //
  49. #define TR ((ULONG)(1000l * 1000l))
  50. static ULONG
  51. Pow2(
  52. UCHAR Exponent
  53. )
  54. {
  55. ULONG result = 1;
  56. while(Exponent--)
  57. result *= 2;
  58. return result;
  59. }
  60. #ifdef _X86_
  61. #pragma optimize("", off)
  62. #endif
  63. #if (DEBUG && DEBUG_VERBOSE)
  64. #pragma message("Debug Verbose is turned on")
  65. ULONG DebugLevel = DEBUG_PERF | DEBUG_ATR;
  66. #else
  67. ULONG DebugLevel = 0;
  68. #endif
  69. ULONG
  70. #ifdef SMCLIB_VXD
  71. SMCLIB_SmartcardGetDebugLevel(
  72. #else
  73. SmartcardGetDebugLevel(
  74. #endif
  75. void
  76. )
  77. {
  78. return DebugLevel;
  79. }
  80. void
  81. #ifdef SMCLIB_VXD
  82. SMCLIB_SmartcardSetDebugLevel(
  83. #else
  84. SmartcardSetDebugLevel(
  85. #endif
  86. ULONG Level
  87. )
  88. {
  89. DebugLevel = Level;
  90. }
  91. #ifdef _X86_
  92. #pragma optimize("", on)
  93. #endif
  94. #if DEBUG
  95. void
  96. DumpData(
  97. const ULONG DebugLevel,
  98. PUCHAR Data,
  99. ULONG DataLen
  100. )
  101. {
  102. ULONG i, line = 0;
  103. TCHAR buffer[72], *pbuffer;
  104. while(DataLen) {
  105. pbuffer = buffer;
  106. sprintf(pbuffer, "%*s", sizeof(buffer) - 1, "");
  107. if (line > 0) {
  108. pbuffer += 8;
  109. }
  110. for (i = 0; i < 8 && DataLen; i++, DataLen--, Data++) {
  111. sprintf(pbuffer + i * 3, "%02X ", *Data);
  112. sprintf(pbuffer + i + 26, "%c", (isprint(*Data) ? *Data : '.'));
  113. }
  114. pbuffer[i * 3] = ' ';
  115. pbuffer[i + 26] = '\n';
  116. pbuffer[i + 27] = '\0';
  117. SmartcardDebug(DebugLevel, (buffer));
  118. line += 1;
  119. }
  120. }
  121. #endif
  122. VOID
  123. SmartcardInitializeCardCapabilities(
  124. PSMARTCARD_EXTENSION SmartcardExtension
  125. )
  126. /*++
  127. Routine Description:
  128. This routine initializes all fields in the CardCapabilities structure,
  129. which can be calculated by the SmartcardUpdateCardCapabilities function.
  130. Arguments:
  131. SmartcardExtension
  132. Return Value:
  133. None
  134. --*/
  135. {
  136. //
  137. // Save the pointers to the two tables
  138. //
  139. PCLOCK_RATE_CONVERSION ClockRateConversion = SmartcardExtension->CardCapabilities.ClockRateConversion;
  140. PBIT_RATE_ADJUSTMENT BitRateAdjustment = SmartcardExtension->CardCapabilities.BitRateAdjustment;
  141. //
  142. // Right now it is fine to zero the whole struct
  143. //
  144. RtlZeroMemory(
  145. &SmartcardExtension->CardCapabilities,
  146. sizeof(SmartcardExtension->CardCapabilities)
  147. );
  148. // Restore the pointers
  149. SmartcardExtension->CardCapabilities.ClockRateConversion = ClockRateConversion;
  150. SmartcardExtension->CardCapabilities.BitRateAdjustment = BitRateAdjustment;
  151. //
  152. // Every card has to support the 'raw' protocol
  153. // It enables the usage of cards that have their own protocol defined
  154. //
  155. SmartcardExtension->CardCapabilities.Protocol.Supported =
  156. SCARD_PROTOCOL_RAW;
  157. //
  158. // Reset T=1 specific data
  159. //
  160. // force the T=1 protocol to start with an ifsd request
  161. SmartcardExtension->T1.State = T1_INIT;
  162. //
  163. // Initialize the send sequence number and
  164. // the 'receive sequence number'
  165. //
  166. SmartcardExtension->T1.SSN = 0;
  167. SmartcardExtension->T1.RSN = 0;
  168. SmartcardExtension->T1.IFSC = 0;
  169. ASSERT(SmartcardExtension->ReaderCapabilities.MaxIFSD != 0);
  170. // Initialize the interface information field size
  171. if (SmartcardExtension->ReaderCapabilities.MaxIFSD != 0 &&
  172. SmartcardExtension->ReaderCapabilities.MaxIFSD <= T1_IFSD) {
  173. SmartcardExtension->T1.IFSD =
  174. (UCHAR) SmartcardExtension->ReaderCapabilities.MaxIFSD;
  175. } else {
  176. SmartcardExtension->T1.IFSD = T1_IFSD_DEFAULT;
  177. }
  178. }
  179. VOID
  180. SmartcardInvertData(
  181. PUCHAR Buffer,
  182. ULONG Length
  183. )
  184. /*++
  185. Routine Description:
  186. This routine converts the passed buffer from inverse to direct or the other way
  187. Arguments:
  188. Return Value:
  189. None
  190. --*/
  191. {
  192. ULONG i;
  193. for (i = 0; i < Length; i++) {
  194. UCHAR j, inv = 0;
  195. for (j = 0; j < 8; j++) {
  196. if (Buffer[i] & (1 << j)) {
  197. inv |= 1 << (7 - j);
  198. }
  199. }
  200. Buffer[i] = (inv ^ 0xFF);
  201. }
  202. }
  203. NTSTATUS
  204. #ifdef SMCLIB_VXD
  205. SMCLIB_SmartcardUpdateCardCapabilities(
  206. #else
  207. SmartcardUpdateCardCapabilities(
  208. #endif
  209. PSMARTCARD_EXTENSION SmartcardExtension
  210. )
  211. /*++
  212. Routine Description:
  213. This routine updates the CardCapabilities structure, which holds information about
  214. the smartcard that has just been reset and is currently in use. It reads the
  215. ATR string and retrieves all the relevent information.
  216. Please refer to ISO 7816-3 ,section 6.1.4 for the format of the ATR string
  217. Arguments:
  218. SmartcardExtension
  219. Return Value:
  220. NTSTATUS
  221. --*/
  222. {
  223. PSCARD_CARD_CAPABILITIES cardCapabilities = &SmartcardExtension->CardCapabilities;
  224. PSCARD_READER_CAPABILITIES readerCapabilities = &SmartcardExtension->ReaderCapabilities;
  225. PUCHAR atrString = cardCapabilities->ATR.Buffer;
  226. ULONG atrLength = (ULONG) cardCapabilities->ATR.Length;
  227. UCHAR Y, Tck, TA[MAXIMUM_ATR_CODES], TB[MAXIMUM_ATR_CODES];
  228. UCHAR TC[MAXIMUM_ATR_CODES], TD[MAXIMUM_ATR_CODES];
  229. ULONG i, fs, numProtocols = 0, protocolTypes = 0;
  230. NTSTATUS status = STATUS_SUCCESS;
  231. BOOLEAN TA2Present = FALSE;
  232. #if DEBUG
  233. TCHAR *ptsType[] = {TEXT("PTS_TYPE_DEFAULT"), TEXT("PTS_TYPE_OPTIMAL"), TEXT("PTS_TYPE_USER")};
  234. #endif
  235. #if defined (SMCLIB_NT)
  236. KIRQL irql;
  237. #endif
  238. SmartcardDebug(
  239. DEBUG_ATR,
  240. (TEXT("%s!SmartcardUpdateCardCapabilities:\n"),
  241. DRIVER_NAME)
  242. );
  243. if (atrLength < 2) {
  244. SmartcardDebug(
  245. DEBUG_ATR,
  246. (TEXT(" ATR is too short (Min. length is 2) \n"))
  247. );
  248. return STATUS_UNRECOGNIZED_MEDIA;
  249. }
  250. #if DEBUG
  251. SmartcardDebug(
  252. DEBUG_ATR,
  253. (TEXT(" ATR: "))
  254. );
  255. DumpData(
  256. DEBUG_ATR,
  257. atrString,
  258. atrLength
  259. );
  260. #endif
  261. if (atrString[0] != 0x3b && atrString[0] != 0x3f && atrString[0] != 0x03) {
  262. SmartcardDebug(
  263. DEBUG_ATR,
  264. (TEXT(" Initial character %02xh of ATR is invalid\n"),
  265. atrString[0])
  266. );
  267. return STATUS_UNRECOGNIZED_MEDIA;
  268. }
  269. // Test for invers convention
  270. if (*atrString == 0x03) {
  271. cardCapabilities->InversConvention = TRUE;
  272. //
  273. // When the ATR starts with 0x03 then it
  274. // has not been inverted already
  275. //
  276. SmartcardInvertData(
  277. cardCapabilities->ATR.Buffer,
  278. cardCapabilities->ATR.Length
  279. );
  280. SmartcardDebug(
  281. DEBUG_ATR,
  282. (TEXT(" Card uses Inverse Convention\n"))
  283. );
  284. }
  285. __try {
  286. //
  287. // The caller might be calling this function repeatedly in order to
  288. // test if the ATR is valid. If the ATR we currently have here is
  289. // not valid then we need to be able re-invert an inverted ATR.
  290. //
  291. atrString += 1;
  292. atrLength -= 1;
  293. //
  294. // Calculate check char, but do not test now since if only T=0
  295. // is present the ATR doesn't contain a check char
  296. //
  297. for (i = 0, Tck = 0; i < atrLength; i++) {
  298. Tck ^= atrString[i];
  299. }
  300. // Initialize various data
  301. cardCapabilities->Protocol.Supported = 0;
  302. RtlZeroMemory(TA, sizeof(TA));
  303. RtlZeroMemory(TB, sizeof(TB));
  304. RtlZeroMemory(TC, sizeof(TC));
  305. RtlZeroMemory(TD, sizeof(TD));
  306. //
  307. // Set default values as described in ISO 7816-3
  308. //
  309. // TA1 codes Fl in high-byte and Dl in low-byte;
  310. TA[0] = 0x11;
  311. // TB1 codes II in bits b7/b6 and Pl1 in b5-b1. b8 has to be 0
  312. TB[0] = 0x25;
  313. // TC2 codes T=0 WI
  314. TC[1] = 10;
  315. // Translate ATR string to TA to TD values (See ISO)
  316. cardCapabilities->HistoricalChars.Length = *atrString & 0x0f;
  317. Y = *atrString++ & 0xf0;
  318. atrLength -= 1;
  319. for (i = 0; i < MAXIMUM_ATR_CODES; i++) {
  320. if (Y & 0x10) {
  321. if (i == 1) {
  322. TA2Present = TRUE;
  323. }
  324. TA[i] = *atrString++;
  325. atrLength -= 1;
  326. }
  327. if (Y & 0x20) {
  328. TB[i] = *atrString++;
  329. atrLength -= 1;
  330. }
  331. if (Y & 0x40) {
  332. TC[i] = *atrString++;
  333. atrLength -= 1;
  334. }
  335. if (Y & 0x80) {
  336. Y = *atrString & 0xf0;
  337. TD[i] = *atrString++ & 0x0f;
  338. atrLength -= 1;
  339. // Check if the next parameters are for a new protocol.
  340. if (((1 << TD[i]) & protocolTypes) == 0) {
  341. // Count the number of protocols that the card supports
  342. numProtocols++;
  343. }
  344. protocolTypes |= 1 << TD[i];
  345. } else {
  346. break;
  347. }
  348. }
  349. // Check if the card supports a protocol other than T=0
  350. if (protocolTypes & ~1) {
  351. //
  352. // The atr contains a checksum byte.
  353. // Exclude that from the historical byte length check
  354. //
  355. atrLength -=1;
  356. //
  357. // This card supports more than one protocol or a protocol
  358. // other than T=0, so test if the checksum is correct
  359. //
  360. if (Tck != 0) {
  361. SmartcardDebug(
  362. DEBUG_ATR,
  363. (TEXT(" ATR Checksum is invalid\n"))
  364. );
  365. status = STATUS_UNRECOGNIZED_MEDIA;
  366. __leave;
  367. }
  368. }
  369. if (/* atrLength < 0 || */
  370. atrLength != cardCapabilities->HistoricalChars.Length) {
  371. SmartcardDebug(
  372. DEBUG_ATR,
  373. (TEXT(" ATR length is inconsistent\n"))
  374. );
  375. status = STATUS_UNRECOGNIZED_MEDIA;
  376. __leave;
  377. }
  378. }
  379. __finally {
  380. if (status != STATUS_SUCCESS) {
  381. if (cardCapabilities->InversConvention == TRUE) {
  382. SmartcardInvertData(
  383. cardCapabilities->ATR.Buffer,
  384. cardCapabilities->ATR.Length
  385. );
  386. cardCapabilities->InversConvention = FALSE;
  387. }
  388. }
  389. }
  390. if (status != STATUS_SUCCESS)
  391. return status;
  392. // store historical characters
  393. RtlCopyMemory(
  394. cardCapabilities->HistoricalChars.Buffer,
  395. atrString,
  396. cardCapabilities->HistoricalChars.Length
  397. );
  398. //
  399. // Now convert TA - TD values to global interface bytes
  400. //
  401. // Clock rate conversion
  402. cardCapabilities->Fl = (TA[0] & 0xf0) >> 4;
  403. // bit rate adjustment
  404. cardCapabilities->Dl = (TA[0] & 0x0f);
  405. // Maximum programming current factor
  406. cardCapabilities->II = (TB[0] & 0xc0) >> 6;
  407. // Programming voltage in 0.1 Volts
  408. cardCapabilities->P = (TB[1] ? TB[1] : (TB[0] & 0x1f) * 10);
  409. // Extra guard time
  410. cardCapabilities->N = TC[0];
  411. //
  412. // Check if the Dl and Fl values are valid
  413. //
  414. if (BitRateAdjustment[cardCapabilities->Dl].DNumerator == 0 ||
  415. ClockRateConversion[cardCapabilities->Fl].F == 0) {
  416. SmartcardDebug(
  417. DEBUG_ATR,
  418. (TEXT(" Dl = %02x or Fl = %02x invalid\n"),
  419. cardCapabilities->Dl,
  420. cardCapabilities->Fl)
  421. );
  422. return STATUS_UNRECOGNIZED_MEDIA;
  423. }
  424. ASSERT(readerCapabilities->CLKFrequency.Max != 0);
  425. ASSERT(readerCapabilities->CLKFrequency.Default != 0);
  426. SmartcardDebug(
  427. DEBUG_ATR,
  428. (TEXT(" Card parameters from ATR:\n Fl = %02x (%ld KHz), Dl = %02x, I = %02x, P = %02x, N = %02x\n"),
  429. cardCapabilities->Fl,
  430. ClockRateConversion[cardCapabilities->Fl].fs / 1000,
  431. cardCapabilities->Dl,
  432. cardCapabilities->II,
  433. cardCapabilities->P,
  434. cardCapabilities->N)
  435. );
  436. //
  437. // assume default clock frequency
  438. //
  439. fs = readerCapabilities->CLKFrequency.Default * 1000l;
  440. if (fs == 0) {
  441. fs = 372 * 9600l;
  442. }
  443. if (cardCapabilities->PtsData.Type == PTS_TYPE_DEFAULT) {
  444. //
  445. // Assume default parameters
  446. //
  447. cardCapabilities->PtsData.Fl = 1;
  448. cardCapabilities->PtsData.Dl = 1;
  449. cardCapabilities->PtsData.DataRate =
  450. readerCapabilities->DataRate.Default;
  451. cardCapabilities->PtsData.CLKFrequency =
  452. readerCapabilities->CLKFrequency.Default;
  453. }
  454. if (cardCapabilities->PtsData.Type != PTS_TYPE_DEFAULT) {
  455. //
  456. // Try to find optimal parameters:
  457. // Highest possible clock frequency of the card
  458. // combined with fastes data rate
  459. //
  460. //
  461. // We now try to find a working Fl and Dl combination
  462. //
  463. if (cardCapabilities->PtsData.Type == PTS_TYPE_OPTIMAL) {
  464. cardCapabilities->PtsData.Fl = cardCapabilities->Fl;
  465. }
  466. ASSERT(cardCapabilities->PtsData.Fl < 16);
  467. ASSERT(ClockRateConversion[cardCapabilities->PtsData.Fl].F);
  468. if (cardCapabilities->PtsData.Fl > 15 ||
  469. ClockRateConversion[cardCapabilities->PtsData.Fl].F == 0) {
  470. return STATUS_INVALID_PARAMETER;
  471. }
  472. do {
  473. ULONG cardFreq, maxFreq;
  474. if (readerCapabilities->CLKFrequenciesSupported.Entries == 0 ||
  475. readerCapabilities->CLKFrequenciesSupported.List == NULL) {
  476. //
  477. // The clock freq. list supplied by the reader is empty
  478. // We take the standard values supplied by the reader
  479. //
  480. readerCapabilities->CLKFrequenciesSupported.List =
  481. &readerCapabilities->CLKFrequency.Default;
  482. readerCapabilities->CLKFrequenciesSupported.Entries = 2;
  483. }
  484. //
  485. // Find the highest possible clock freq. supported
  486. // by the card and the reader
  487. //
  488. cardFreq =
  489. ClockRateConversion[cardCapabilities->PtsData.Fl].fs /
  490. 1000;
  491. cardCapabilities->PtsData.CLKFrequency = 0;
  492. for (i = 0; i < readerCapabilities->CLKFrequenciesSupported.Entries; i++) {
  493. // look for highest possible reader frequency
  494. if (readerCapabilities->CLKFrequenciesSupported.List[i] >
  495. cardCapabilities->PtsData.CLKFrequency &&
  496. readerCapabilities->CLKFrequenciesSupported.List[i] <=
  497. cardFreq) {
  498. cardCapabilities->PtsData.CLKFrequency =
  499. readerCapabilities->CLKFrequenciesSupported.List[i];
  500. }
  501. }
  502. fs = cardCapabilities->PtsData.CLKFrequency * 1000;
  503. cardCapabilities->PtsData.DataRate = 0;
  504. ASSERT(fs != 0);
  505. if (fs == 0) {
  506. return STATUS_INVALID_PARAMETER;
  507. }
  508. if (cardCapabilities->PtsData.Type == PTS_TYPE_OPTIMAL) {
  509. cardCapabilities->PtsData.Dl = cardCapabilities->Dl;
  510. }
  511. ASSERT(cardCapabilities->PtsData.Dl < 16);
  512. ASSERT(BitRateAdjustment[cardCapabilities->PtsData.Dl].DNumerator);
  513. if (cardCapabilities->PtsData.Dl > 15 ||
  514. BitRateAdjustment[cardCapabilities->PtsData.Dl].DNumerator == 0) {
  515. return STATUS_INVALID_PARAMETER;
  516. }
  517. if (readerCapabilities->DataRatesSupported.Entries == 0 ||
  518. readerCapabilities->DataRatesSupported.List == NULL) {
  519. //
  520. // The data rate list supplied by the reader is empty.
  521. // We take the standard min/max values of the reader
  522. //
  523. readerCapabilities->DataRatesSupported.List =
  524. &readerCapabilities->DataRate.Default;
  525. readerCapabilities->DataRatesSupported.Entries = 2;
  526. }
  527. //
  528. // Now try to find the highest possible matching data rate
  529. // (A matching data rate is one that VERY close
  530. // to one supplied by the reader)
  531. //
  532. while(cardCapabilities->PtsData.Dl > 1) {
  533. ULONG dataRate;
  534. //
  535. // Calculate the data rate using the current values
  536. //
  537. dataRate =
  538. (BitRateAdjustment[cardCapabilities->PtsData.Dl].DNumerator *
  539. fs) /
  540. (BitRateAdjustment[cardCapabilities->PtsData.Dl].DDivisor *
  541. ClockRateConversion[cardCapabilities->PtsData.Fl].F);
  542. //
  543. // Try to find a matching data rate
  544. //
  545. for (i = 0; i < readerCapabilities->DataRatesSupported.Entries; i++) {
  546. if (readerCapabilities->DataRatesSupported.List[i] * 101 > dataRate * 100 &&
  547. readerCapabilities->DataRatesSupported.List[i] * 99 < dataRate * 100) {
  548. cardCapabilities->PtsData.DataRate =
  549. readerCapabilities->DataRatesSupported.List[i];
  550. break;
  551. }
  552. }
  553. if (cardCapabilities->PtsData.DataRate) {
  554. break;
  555. }
  556. //
  557. // Select the next valid lower D value
  558. //
  559. while (BitRateAdjustment[--cardCapabilities->PtsData.Dl].DNumerator == 0)
  560. ;
  561. }
  562. if (cardCapabilities->PtsData.Fl == 1 &&
  563. cardCapabilities->PtsData.Dl == 1) {
  564. cardCapabilities->PtsData.DataRate =
  565. readerCapabilities->DataRate.Default;
  566. cardCapabilities->PtsData.CLKFrequency =
  567. readerCapabilities->CLKFrequency.Default;
  568. break;
  569. }
  570. if (cardCapabilities->PtsData.DataRate) {
  571. break;
  572. }
  573. //
  574. // Select the next valid lower F value
  575. //
  576. maxFreq = ClockRateConversion[cardCapabilities->Fl].fs;
  577. do {
  578. cardCapabilities->PtsData.Fl -= 1;
  579. } while (ClockRateConversion[cardCapabilities->PtsData.Fl].F == 0 ||
  580. ClockRateConversion[cardCapabilities->PtsData.Fl].fs >
  581. maxFreq);
  582. } while(cardCapabilities->PtsData.DataRate == 0);
  583. }
  584. ASSERT(fs != 0);
  585. ASSERT(cardCapabilities->PtsData.Dl < 16);
  586. ASSERT(BitRateAdjustment[cardCapabilities->PtsData.Dl].DNumerator != 0);
  587. //
  588. // We calculate the ETU on basis of the timing supplied by the
  589. // clk-frequency of the reader
  590. //
  591. //
  592. // Work etu in units of time resolution(TR) (NOT in seconds)
  593. //
  594. cardCapabilities->etu =
  595. 1 + // required to round up
  596. (TR *
  597. BitRateAdjustment[cardCapabilities->PtsData.Dl].DDivisor *
  598. ClockRateConversion[cardCapabilities->PtsData.Fl].F) /
  599. (BitRateAdjustment[cardCapabilities->PtsData.Dl].DNumerator *
  600. fs);
  601. //
  602. // guard time in micro seconds
  603. // the guard time is the gap between the end of the
  604. // current character and the beginning of the next character
  605. //
  606. cardCapabilities->GT = 0;
  607. cardCapabilities->PtsData.StopBits = 2;
  608. if (cardCapabilities->N == 255) {
  609. cardCapabilities->PtsData.StopBits = 1;
  610. } else if (cardCapabilities->N > 0) {
  611. cardCapabilities->GT = cardCapabilities->N * cardCapabilities->etu;
  612. }
  613. SmartcardDebug(
  614. DEBUG_ATR,
  615. (TEXT(" PTS parameters (%s):\n Fl = %02x (%ld KHz), Dl = %02x (%ld Bps, %d Stop Bits)\n"),
  616. ptsType[cardCapabilities->PtsData.Type],
  617. cardCapabilities->PtsData.Fl,
  618. cardCapabilities->PtsData.CLKFrequency,
  619. cardCapabilities->PtsData.Dl,
  620. cardCapabilities->PtsData.DataRate,
  621. cardCapabilities->PtsData.StopBits)
  622. );
  623. SmartcardDebug(
  624. DEBUG_ATR,
  625. (TEXT(" Calculated timing values:\n Work etu = %ld micro sec, Guard time = %ld micro sec\n"),
  626. cardCapabilities->etu,
  627. cardCapabilities->GT)
  628. );
  629. #if defined (SMCLIB_NT) && !defined(SMCLIB_TEST)
  630. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock, &irql);
  631. #endif
  632. if(SmartcardExtension->ReaderCapabilities.CurrentState >= SCARD_PRESENT) {
  633. if (TA2Present || numProtocols <= 1 &&
  634. cardCapabilities->Fl == 1 &&
  635. cardCapabilities->Dl == 1) {
  636. //
  637. // If the card supports only one protocol (or T=0 as default)
  638. // and only standard paramters then PTS selection is not available
  639. //
  640. SmartcardExtension->ReaderCapabilities.CurrentState =
  641. SCARD_SPECIFIC;
  642. } else {
  643. SmartcardExtension->ReaderCapabilities.CurrentState =
  644. SCARD_NEGOTIABLE;
  645. }
  646. }
  647. #if defined (SMCLIB_NT) && !defined(SMCLIB_TEST)
  648. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock, irql);
  649. #endif
  650. //
  651. // Now find protocol specific data
  652. //
  653. if (TD[0] == 0) {
  654. cardCapabilities->Protocol.Supported |=
  655. SCARD_PROTOCOL_T0;
  656. cardCapabilities->T0.WI = TC[1];
  657. if (cardCapabilities->PtsData.Dl > 0 &&
  658. cardCapabilities->PtsData.Dl < 6) {
  659. cardCapabilities->T0.WT = 1 +
  660. cardCapabilities->T0.WI *
  661. 960 * cardCapabilities->etu *
  662. Pow2((UCHAR) (cardCapabilities->PtsData.Dl - 1));
  663. } else {
  664. cardCapabilities->T0.WT = 1+
  665. cardCapabilities->T0.WI *
  666. 960 * cardCapabilities->etu /
  667. Pow2((UCHAR) (cardCapabilities->PtsData.Dl - 1));
  668. }
  669. SmartcardDebug(
  670. DEBUG_ATR,
  671. (TEXT(" T=0 Values from ATR:\n WI = %ld\n"),
  672. cardCapabilities->T0.WI)
  673. );
  674. SmartcardDebug(
  675. DEBUG_ATR,
  676. (TEXT(" T=0 Timing from ATR:\n WT = %ld ms\n"),
  677. cardCapabilities->T0.WT / 1000)
  678. );
  679. }
  680. if (protocolTypes & SCARD_PROTOCOL_T1) {
  681. for (i = 0; TD[i] != 1 && i < MAXIMUM_ATR_CODES; i++)
  682. ;
  683. for (; TD[i] == 1 && i < MAXIMUM_ATR_CODES; i++)
  684. ;
  685. if (i == MAXIMUM_ATR_CODES) {
  686. return STATUS_UNRECOGNIZED_MEDIA;
  687. }
  688. cardCapabilities->Protocol.Supported |=
  689. SCARD_PROTOCOL_T1;
  690. cardCapabilities->T1.IFSC =
  691. (TA[i] ? TA[i] : 32);
  692. cardCapabilities->T1.CWI =
  693. ((TB[i] & 0x0f) ? (TB[i] & 0x0f) : T1_CWI_DEFAULT);
  694. cardCapabilities->T1.BWI =
  695. ((TB[i] & 0xf0) >> 4 ? (TB[i] & 0xf0) >> 4 : T1_BWI_DEFAULT);
  696. cardCapabilities->T1.EDC =
  697. (TC[i] & 0x01);
  698. cardCapabilities->T1.CWT = 1 +
  699. (Pow2(cardCapabilities->T1.CWI) + 11) * cardCapabilities->etu;
  700. cardCapabilities->T1.BWT = 1 +
  701. (((Pow2(cardCapabilities->T1.BWI) * 960l * 372l) /
  702. cardCapabilities->PtsData.CLKFrequency) +
  703. (11 * cardCapabilities->etu)) * 1000;
  704. SmartcardDebug(
  705. DEBUG_ATR,
  706. (TEXT(" T=1 Values from ATR:\n IFSC = %ld, CWI = %ld, BWI = %ld, EDC = %02x\n"),
  707. cardCapabilities->T1.IFSC,
  708. cardCapabilities->T1.CWI,
  709. cardCapabilities->T1.BWI,
  710. cardCapabilities->T1.EDC)
  711. );
  712. SmartcardDebug(
  713. DEBUG_ATR,
  714. (TEXT(" T=1 Timing from ATR:\n CWT = %ld ms, BWT = %ld ms\n"),
  715. cardCapabilities->T1.CWT / 1000,
  716. cardCapabilities->T1.BWT / 1000)
  717. );
  718. }
  719. if (SmartcardExtension->ReaderCapabilities.CurrentState == SCARD_SPECIFIC) {
  720. if (TA2Present) {
  721. //
  722. // TA2 is present in the ATR, so use
  723. // the protocol indicated in the ATR
  724. //
  725. cardCapabilities->Protocol.Selected = 1 << (TA[1] & 0xf);
  726. } else {
  727. //
  728. // The card only supports one protocol
  729. // So make that one protocol the current one to use
  730. //
  731. cardCapabilities->Protocol.Selected =
  732. cardCapabilities->Protocol.Supported;
  733. }
  734. SmartcardDebug(
  735. DEBUG_ATR,
  736. (TEXT(" Mode: Specific %s\n\n"),
  737. TA2Present ? TEXT("set by TA(2)") : TEXT(""))
  738. );
  739. } else {
  740. SmartcardDebug(
  741. DEBUG_ATR,
  742. (TEXT(" Mode: Negotiable\n\n"))
  743. );
  744. }
  745. //
  746. // Every card has to support the 'raw' protocol
  747. // It enables the usage of cards that have their own protocol defined
  748. //
  749. SmartcardExtension->CardCapabilities.Protocol.Supported |=
  750. SCARD_PROTOCOL_RAW;
  751. return STATUS_SUCCESS;
  752. }
  753. #ifdef SMCLIB_TEST
  754. __cdecl
  755. main()
  756. {
  757. SMARTCARD_EXTENSION SmartcardExtension;
  758. static ULONG dataRatesSupported[] = { 9909 };
  759. memset(&SmartcardExtension, 0, sizeof(SmartcardExtension));
  760. // Gemplus T=0 card
  761. // memcpy(SmartcardExtension.CardCapabilities.ATR.Buffer, "\x3b\x2a\x00\x80\x65\xa2\x01\x02\x01\x31\x72\xd6\x43", 13);
  762. // SmartcardExtension.CardCapabilities.ATR.Length = 13;
  763. // MS card with TA2 set - card won't work due to incorrect parameters
  764. memcpy(SmartcardExtension.CardCapabilities.ATR.Buffer, "\x3b\x98\x13\x91\x81\x31\x20\x55\x00\x57\x69\x6e\x43\x61\x72\x64\xbb", 17);
  765. SmartcardExtension.CardCapabilities.ATR.Length = 17;
  766. SmartcardExtension.ReaderCapabilities.CLKFrequency.Default = 3686;
  767. SmartcardExtension.ReaderCapabilities.CLKFrequency.Max = 3686;
  768. SmartcardExtension.ReaderCapabilities.DataRate.Default =
  769. SmartcardExtension.ReaderCapabilities.DataRate.Max = 9909;
  770. SmartcardExtension.ReaderCapabilities.DataRatesSupported.List =
  771. dataRatesSupported;
  772. SmartcardExtension.ReaderCapabilities.DataRatesSupported.Entries =
  773. sizeof(dataRatesSupported) / sizeof(dataRatesSupported[0]);
  774. SmartcardExtension.ReaderCapabilities.CurrentState = SCARD_PRESENT;
  775. SmartcardSetDebugLevel(DEBUG_ALL);
  776. SmartcardUpdateCardCapabilities(&SmartcardExtension);
  777. }
  778. #define DbgPrint printf
  779. #endif