-
Notifications
You must be signed in to change notification settings - Fork 0
/
winscard.c
1655 lines (1418 loc) · 40 KB
/
winscard.c
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MUSCLE SmartCard Development ( http:https://pcsclite.alioth.debian.org/pcsclite.html )
*
* Copyright (C) 1999-2004
* David Corcoran <[email protected]>
* Copyright (C) 2002-2011
* Ludovic Rousseau <[email protected]>
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: winscard.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @mainpage MUSCLE PC/SC-Lite API Documentation
*
* @section Introduction
*
* This document contains the reference API calls for communicating to the
* MUSCLE PC/SC Smart Card Resource Manager. PC/SC is a standard proposed by
* the PC/SC workgroup http:https://www.pcscworkgroup.com/ which is a conglomerate of
* representative from major smart card manufacturers and other companies. This
* specification tries to abstract the smart card layer into a high level API
* so that smart cards and their readers can be accessed in a homogeneous
* fashion.
*
* This toolkit was written in ANSI C that can be used with most compilers and
* does NOT use complex and large data structures such as vectors, etc. The C
* API emulates the winscard API that is used on the Windows platform. It is
* contained in the library <tt>libpcsclite.so</tt> that is linked to your
* application.
*
* I would really like to hear from you. If you have any feedback either on
* this documentation or on the MUSCLE project please feel free to email me at:
*
*
* @section API_Routines
*
* These routines specified here are winscard.h routines like those in the
* winscard API provided under Windows(R). These are compatible with the
* Microsoft(R) API calls. This list of calls is mainly an abstraction of
* readers. It gives a common API for communication to most readers in a
* homogeneous fashion.
*
* Since all functions can produce a wide array of errors, please refer to
* pcsclite.h for a list of error returns.
*
* For a human readable representation of an error the function
* pcsc_stringify_error() is declared in pcsclite.h. This function is not
* available on Microsoft(R) winscard API and is pcsc-lite specific.
*
* @section Internals
*
* PC/SC Lite is formed by a server deamon (<tt>pcscd</tt>) and a client
* library (<tt>libpcsclite.so</tt>) that communicate via IPC.
*
* The file \em winscard_clnt.c in the client-side exposes the API for
* applications.\n The file \em winscard.c has the server-side counterpart
* functions present in \em winscard_clnt.c.\n The file \em winscard_msg.c is
* the communication interface between \em winscard_clnt.c and \em
* winscard.c.\n The file pcscdaemon.c has the main server-side function,
* including a loop for accepting client requests.\n The file \em
* winscard_svc.c has the functions called by \em pcscdaemon.c to serve clients
* requests.
*
* When a function from \em winscard_clnt.c is called by a client application,
* it calls a function in \em winscard_msg.c to send the message to \em
* pcscdaemon.c. When \em pcscdaemon.c a client detects a request arrived, it
* calls \em winscard_svc.c which identifies what command the message contains
* and requests \em winscard.c to execute the command.\n Meanwhile
* winscard_clnt.c waits for the response until a timeout occurs.
*/
/**
* @file
* @brief This handles smart card reader communications.
* This is the heart of the smart card API.
*
* Here are the main server-side functions which execute the requests from the
* clients.
*/
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <pthread.h>
#include <PCSC/pcscd.h>
#include <PCSC/winscard.h>
#include <PCSC/ifdhandler.h>
#include <PCSC/debuglog.h>
#include <PCSC/readerfactory.h>
#include <PCSC/prothandler.h>
#include <PCSC/ifdwrapper.h>
#include <PCSC/atrhandler.h>
#include <PCSC/sys_generic.h>
#include <PCSC/eventhandler.h>
#include <PCSC/utils.h>
#include <PCSC/reader.h>
#include <PCSC/strlcpycat.h>
#undef DO_PROFILE
#ifdef DO_PROFILE
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
#define PROFILE_FILE "/tmp/pcscd_profile"
#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
struct timeval profile_time_start;
FILE *fd;
char profile_tty;
#define PROFILE_START profile_start(__FUNCTION__);
#define PROFILE_END profile_end(__FUNCTION__, __LINE__);
static void profile_start(const char *f)
{
static char initialized = FALSE;
if (!initialized)
{
initialized = TRUE;
fd = fopen(PROFILE_FILE, "a+");
if (NULL == fd)
{
fprintf(stderr, "\33[01;31mCan't open %s: %s\33[0m\n",
PROFILE_FILE, strerror(errno));
exit(-1);
}
fprintf(fd, "\nStart a new profile\n");
fflush(fd);
if (isatty(fileno(stderr)))
profile_tty = TRUE;
else
profile_tty = FALSE;
}
gettimeofday(&profile_time_start, NULL);
} /* profile_start */
static void profile_end(const char *f, int line)
{
struct timeval profile_time_end;
long d;
gettimeofday(&profile_time_end, NULL);
d = time_sub(&profile_time_end, &profile_time_start);
if (profile_tty)
fprintf(stderr, "\33[01;31mRESULT %s \33[35m%ld\33[0m (%d)\n", f, d,
line);
fprintf(fd, "%s %ld\n", f, d);
fflush(fd);
} /* profile_end */
#else
#define PROFILE_START
#define PROFILE_END
#endif
/** used for backward compatibility */
#define SCARD_PROTOCOL_ANY_OLD 0x1000
//LONG SCardEstablishContext(DWORD dwScope, /*@unused@*/ LPCVOID pvReserved1,
// /*@unused@*/ LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
//{
// (void)pvReserved1;
// (void)pvReserved2;
//
// if (dwScope != SCARD_SCOPE_USER && dwScope != SCARD_SCOPE_TERMINAL &&
// dwScope != SCARD_SCOPE_SYSTEM && dwScope != SCARD_SCOPE_GLOBAL)
// {
// *phContext = 0;
// return SCARD_E_INVALID_VALUE;
// }
//
// /*
// * Unique identifier for this server so that it can uniquely be
// * identified by clients and distinguished from others
// */
//
// *phContext = SYS_RandomInt(0, -1);
// *phContext = 0;
//
// Log2(PCSC_LOG_DEBUG, "Establishing Context: 0x%lX", *phContext);
//
// return SCARD_S_SUCCESS;
//}
LONG SCardReleaseContext(SCARDCONTEXT hContext)
{
/*
* Nothing to do here RPC layer will handle this
*/
Log2(PCSC_LOG_DEBUG, "Releasing Context: 0x%lX", hContext);
return SCARD_S_SUCCESS;
}
LONG SCardConnect(/*@unused@*/ SCARDCONTEXT hContext, LPCSTR szReader,
DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
LPDWORD pdwActiveProtocol)
{
LONG rv;
READER_CONTEXT * rContext = NULL;
uint32_t readerState;
(void)hContext;
PROFILE_START
*phCard = 0;
if ((dwShareMode != SCARD_SHARE_DIRECT) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_T0) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_T1) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_RAW) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD))
return SCARD_E_PROTO_MISMATCH;
if (dwShareMode != SCARD_SHARE_EXCLUSIVE &&
dwShareMode != SCARD_SHARE_SHARED &&
dwShareMode != SCARD_SHARE_DIRECT)
return SCARD_E_INVALID_VALUE;
Log3(PCSC_LOG_DEBUG, "Attempting Connect to %s using protocol: %ld",szReader, dwPreferredProtocols);
rv = RFReaderInfo((LPSTR) szReader, &rContext);
if (rv != SCARD_S_SUCCESS)
{
Log2(PCSC_LOG_ERROR, "Reader %s Not Found", szReader);
return rv;
}
/*
* Make sure the reader is working properly
*/
rv = RFCheckReaderStatus(rContext);
if (rv != SCARD_S_SUCCESS)
goto exit;
/*******************************************
*
* This section checks for simple errors
*
*******************************************/
/*
* Connect if not exclusive mode
*/
if (rContext->contexts == PCSCLITE_SHARING_EXCLUSIVE_CONTEXT)
{
Log1(PCSC_LOG_ERROR, "Error Reader Exclusive");
rv = SCARD_E_SHARING_VIOLATION;
goto exit;
}
/*
* wait until a possible transaction is finished
*/
if (rContext->hLockId != 0)
{
Log1(PCSC_LOG_INFO, "Waiting for release of lock");
while (rContext->hLockId != 0)
(void)SYS_USleep(PCSCLITE_LOCK_POLL_RATE);
// Log1(PCSC_LOG_INFO, "Lock released");
}
/*******************************************
*
* This section tries to determine the
* presence of a card or not
*
*******************************************/
readerState = rContext->readerState->readerState;
if (dwShareMode != SCARD_SHARE_DIRECT)
{
if (!(readerState & SCARD_PRESENT))
{
Log1(PCSC_LOG_DEBUG, "Card Not Inserted");
rv = SCARD_E_NO_SMARTCARD;
goto exit;
}
/* Power on (again) the card if needed */
(void)pthread_mutex_lock(&rContext->powerState_lock);
if (POWER_STATE_UNPOWERED == rContext->powerState)
{
DWORD dwAtrLen;
dwAtrLen = sizeof(rContext->readerState->cardAtr);
rv = IFDPowerICC(rContext, IFD_POWER_UP,
rContext->readerState->cardAtr, &dwAtrLen);
rContext->readerState->cardAtrLength = dwAtrLen;
if (rv == IFD_SUCCESS) {
readerState = SCARD_PRESENT | SCARD_POWERED | SCARD_NEGOTIABLE;
Log1(PCSC_LOG_DEBUG, "power up complete.");
LogXxd(PCSC_LOG_DEBUG, "Card ATR: ",
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
} else
Log3(PCSC_LOG_ERROR, "Error powering up card: %ld 0x%04lX", rv, rv);
}
if (! (readerState & SCARD_POWERED))
{
Log1(PCSC_LOG_ERROR, "Card Not Powered");
(void)pthread_mutex_unlock(&rContext->powerState_lock);
rv = SCARD_W_UNPOWERED_CARD;
goto exit;
}
/* the card is now in use */
rContext->powerState = POWER_STATE_INUSE;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_INUSE");
(void)pthread_mutex_unlock(&rContext->powerState_lock);
}
/*******************************************
*
* This section tries to decode the ATR
* and set up which protocol to use
*
*******************************************/
if (dwPreferredProtocols & SCARD_PROTOCOL_RAW)
rContext->readerState->cardProtocol = SCARD_PROTOCOL_RAW;
else
{
if (dwShareMode != SCARD_SHARE_DIRECT)
{
/* lock here instead in IFDSetPTS() to lock up to
* setting rContext->readerState->cardProtocol */
(void)pthread_mutex_lock(rContext->mMutex);
/* the protocol is not yet set (no PPS yet) */
if (SCARD_PROTOCOL_UNDEFINED == rContext->readerState->cardProtocol)
{
int availableProtocols, defaultProtocol;
int ret;
ATRDecodeAtr(&availableProtocols, &defaultProtocol,
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
/* If it is set to ANY let it do any of the protocols */
if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)
dwPreferredProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
ret = PHSetProtocol(rContext, dwPreferredProtocols,
availableProtocols, defaultProtocol);
/* keep cardProtocol = SCARD_PROTOCOL_UNDEFINED in case of error */
if (SET_PROTOCOL_PPS_FAILED == ret)
{
(void)pthread_mutex_unlock(rContext->mMutex);
rv = SCARD_W_UNRESPONSIVE_CARD;
goto exit;
}
if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
{
(void)pthread_mutex_unlock(rContext->mMutex);
rv = SCARD_E_PROTO_MISMATCH;
goto exit;
}
/* use negotiated protocol */
rContext->readerState->cardProtocol = ret;
(void)pthread_mutex_unlock(rContext->mMutex);
}
else
{
(void)pthread_mutex_unlock(rContext->mMutex);
if (! (dwPreferredProtocols & rContext->readerState->cardProtocol))
{
rv = SCARD_E_PROTO_MISMATCH;
goto exit;
}
}
}
}
*pdwActiveProtocol = rContext->readerState->cardProtocol;
if (dwShareMode != SCARD_SHARE_DIRECT)
{
switch (*pdwActiveProtocol)
{
case SCARD_PROTOCOL_T0:
case SCARD_PROTOCOL_T1:
Log2(PCSC_LOG_DEBUG, "Active Protocol: T=%d",
(*pdwActiveProtocol == SCARD_PROTOCOL_T0) ? 0 : 1);
break;
case SCARD_PROTOCOL_RAW:
Log1(PCSC_LOG_DEBUG, "Active Protocol: RAW");
break;
default:
Log2(PCSC_LOG_ERROR, "Active Protocol: unknown %ld",*pdwActiveProtocol);
}
}
else
Log1(PCSC_LOG_DEBUG, "Direct access: no protocol selected");
/*
* Prepare the SCARDHANDLE identity
*/
*phCard = RFCreateReaderHandle(rContext);
Log2(PCSC_LOG_DEBUG, "hCard Identity: %lx", *phCard);
/*******************************************
*
* This section tries to set up the
* exclusivity modes. -1 is exclusive
*
*******************************************/
if (dwShareMode == SCARD_SHARE_EXCLUSIVE)
{
if (rContext->contexts == PCSCLITE_SHARING_NO_CONTEXT)
{
rContext->contexts = PCSCLITE_SHARING_EXCLUSIVE_CONTEXT;
(void)RFLockSharing(*phCard, rContext);
}
else
{
(void)RFDestroyReaderHandle(*phCard);
*phCard = 0;
rv = SCARD_E_SHARING_VIOLATION;
goto exit;
}
}
else
{
/*
* Add a connection to the context stack
*/
rContext->contexts += 1;
}
/*
* Add this handle to the handle list
*/
rv = RFAddReaderHandle(rContext, *phCard);
if (rv != SCARD_S_SUCCESS)
{
/*
* Clean up - there is no more room
*/
(void)RFDestroyReaderHandle(*phCard);
if (rContext->contexts == PCSCLITE_SHARING_EXCLUSIVE_CONTEXT)
rContext->contexts = PCSCLITE_SHARING_NO_CONTEXT;
else
if (rContext->contexts > PCSCLITE_SHARING_NO_CONTEXT)
rContext->contexts -= 1;
*phCard = 0;
rv = SCARD_F_INTERNAL_ERROR;
goto exit;
}
/*
* Propagate new state to reader state
*/
rContext->readerState->readerSharing = rContext->contexts;
exit:
UNREF_READER(rContext)
PROFILE_END
return rv;
}
LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode,
DWORD dwPreferredProtocols, DWORD dwInitialization,
LPDWORD pdwActiveProtocol)
{
LONG rv;
READER_CONTEXT * rContext = NULL;
Log1(PCSC_LOG_DEBUG, "Attempting reconnect to token.");
if (hCard == 0)
return SCARD_E_INVALID_HANDLE;
/*
* Handle the dwInitialization
*/
if (dwInitialization != SCARD_LEAVE_CARD &&
dwInitialization != SCARD_RESET_CARD &&
dwInitialization != SCARD_UNPOWER_CARD)
return SCARD_E_INVALID_VALUE;
if (dwShareMode != SCARD_SHARE_SHARED &&
dwShareMode != SCARD_SHARE_EXCLUSIVE &&
dwShareMode != SCARD_SHARE_DIRECT)
return SCARD_E_INVALID_VALUE;
if ((dwShareMode != SCARD_SHARE_DIRECT) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_T0) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_T1) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_RAW) &&
!(dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD))
return SCARD_E_PROTO_MISMATCH;
/* get rContext corresponding to hCard */
rv = RFReaderInfoById(hCard, &rContext);
if (rv != SCARD_S_SUCCESS)
return rv;
/*
* Make sure the reader is working properly
*/
rv = RFCheckReaderStatus(rContext);
if (rv != SCARD_S_SUCCESS)
goto exit;
/*
* Make sure no one has a lock on this reader
*/
rv = RFCheckSharing(hCard, rContext);
if (rv != SCARD_S_SUCCESS)
goto exit;
if (dwInitialization == SCARD_RESET_CARD ||
dwInitialization == SCARD_UNPOWER_CARD)
{
DWORD dwAtrLen;
/*
* Notify the card has been reset
*/
(void)RFSetReaderEventState(rContext, SCARD_RESET);
/*
* Currently pcsc-lite keeps the card powered constantly
*/
dwAtrLen = sizeof(rContext->readerState->cardAtr);
if (SCARD_RESET_CARD == dwInitialization)
rv = IFDPowerICC(rContext, IFD_RESET,
rContext->readerState->cardAtr, &dwAtrLen);
else
{
IFDPowerICC(rContext, IFD_POWER_DOWN, NULL, NULL);
rv = IFDPowerICC(rContext, IFD_POWER_UP,
rContext->readerState->cardAtr, &dwAtrLen);
}
/* the protocol is unset after a power on */
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
/*
* Set up the status bit masks on readerState
*/
if (rv == SCARD_S_SUCCESS)
{
rContext->readerState->cardAtrLength = dwAtrLen;
rContext->readerState->readerState =
SCARD_PRESENT | SCARD_POWERED | SCARD_NEGOTIABLE;
Log1(PCSC_LOG_DEBUG, "Reset complete.");
LogXxd(PCSC_LOG_DEBUG, "Card ATR: ",
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
}
else
{
rContext->readerState->cardAtrLength = 0;
Log1(PCSC_LOG_ERROR, "Error resetting card.");
if (rv == SCARD_W_REMOVED_CARD)
{
rContext->readerState->readerState = SCARD_ABSENT;
rv = SCARD_E_NO_SMARTCARD;
goto exit;
}
else
{
rContext->readerState->readerState =
SCARD_PRESENT | SCARD_SWALLOWED;
rv = SCARD_W_UNRESPONSIVE_CARD;
goto exit;
}
}
}
else
if (dwInitialization == SCARD_LEAVE_CARD)
{
uint32_t readerState = rContext->readerState->readerState;
if (readerState & SCARD_ABSENT)
{
rv = SCARD_E_NO_SMARTCARD;
goto exit;
}
if ((readerState & SCARD_PRESENT)
&& (readerState & SCARD_SWALLOWED))
{
rv = SCARD_W_UNRESPONSIVE_CARD;
goto exit;
}
}
/*******************************************
*
* This section tries to decode the ATR
* and set up which protocol to use
*
*******************************************/
if (dwPreferredProtocols & SCARD_PROTOCOL_RAW)
rContext->readerState->cardProtocol = SCARD_PROTOCOL_RAW;
else
{
if (dwShareMode != SCARD_SHARE_DIRECT)
{
/* lock here instead in IFDSetPTS() to lock up to
* setting rContext->readerState->cardProtocol */
(void)pthread_mutex_lock(rContext->mMutex);
/* the protocol is not yet set (no PPS yet) */
if (SCARD_PROTOCOL_UNDEFINED == rContext->readerState->cardProtocol)
{
int availableProtocols, defaultProtocol;
int ret;
ATRDecodeAtr(&availableProtocols, &defaultProtocol,
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
/* If it is set to ANY let it do any of the protocols */
if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)
dwPreferredProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
ret = PHSetProtocol(rContext, dwPreferredProtocols,
availableProtocols, defaultProtocol);
/* keep cardProtocol = SCARD_PROTOCOL_UNDEFINED in case of error */
if (SET_PROTOCOL_PPS_FAILED == ret)
{
(void)pthread_mutex_unlock(rContext->mMutex);
rv = SCARD_W_UNRESPONSIVE_CARD;
goto exit;
}
if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
{
(void)pthread_mutex_unlock(rContext->mMutex);
rv = SCARD_E_PROTO_MISMATCH;
goto exit;
}
/* use negotiated protocol */
rContext->readerState->cardProtocol = ret;
(void)pthread_mutex_unlock(rContext->mMutex);
}
else
{
(void)pthread_mutex_unlock(rContext->mMutex);
if (! (dwPreferredProtocols & rContext->readerState->cardProtocol))
{
rv = SCARD_E_PROTO_MISMATCH;
goto exit;
}
}
}
}
*pdwActiveProtocol = rContext->readerState->cardProtocol;
if (dwShareMode != SCARD_SHARE_DIRECT)
{
switch (*pdwActiveProtocol)
{
case SCARD_PROTOCOL_T0:
case SCARD_PROTOCOL_T1:
Log2(PCSC_LOG_DEBUG, "Active Protocol: T=%d",
(*pdwActiveProtocol == SCARD_PROTOCOL_T0) ? 0 : 1);
break;
case SCARD_PROTOCOL_RAW:
Log1(PCSC_LOG_DEBUG, "Active Protocol: RAW");
break;
default:
Log2(PCSC_LOG_ERROR, "Active Protocol: unknown %ld",
*pdwActiveProtocol);
}
}
else
Log1(PCSC_LOG_DEBUG, "Direct access: no protocol selected");
if (dwShareMode == SCARD_SHARE_EXCLUSIVE)
{
if (rContext->contexts == PCSCLITE_SHARING_EXCLUSIVE_CONTEXT)
{
/*
* Do nothing - we are already exclusive
*/
}
else
{
if (rContext->contexts == PCSCLITE_SHARING_LAST_CONTEXT)
{
rContext->contexts = PCSCLITE_SHARING_EXCLUSIVE_CONTEXT;
(void)RFLockSharing(hCard, rContext);
}
else
{
rv = SCARD_E_SHARING_VIOLATION;
goto exit;
}
}
}
else if (dwShareMode == SCARD_SHARE_SHARED)
{
if (rContext->contexts != PCSCLITE_SHARING_EXCLUSIVE_CONTEXT)
{
/*
* Do nothing - in sharing mode already
*/
}
else
{
/*
* We are in exclusive mode but want to share now
*/
(void)RFUnlockSharing(hCard, rContext);
rContext->contexts = PCSCLITE_SHARING_LAST_CONTEXT;
}
}
else if (dwShareMode == SCARD_SHARE_DIRECT)
{
if (rContext->contexts != PCSCLITE_SHARING_EXCLUSIVE_CONTEXT)
{
/*
* Do nothing - in sharing mode already
*/
}
else
{
/*
* We are in exclusive mode but want to share now
*/
(void)RFUnlockSharing(hCard, rContext);
rContext->contexts = PCSCLITE_SHARING_LAST_CONTEXT;
}
}
else
{
rv = SCARD_E_INVALID_VALUE;
goto exit;
}
/*
* Clear a previous event to the application
*/
(void)RFClearReaderEventState(rContext, hCard);
/*
* Propagate new state to reader state
*/
rContext->readerState->readerSharing = rContext->contexts;
rv = SCARD_S_SUCCESS;
exit:
UNREF_READER(rContext)
return rv;
}
LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
{
LONG rv;
READER_CONTEXT * rContext = NULL;
if (hCard == 0)
return SCARD_E_INVALID_HANDLE;
if ((dwDisposition != SCARD_LEAVE_CARD)
&& (dwDisposition != SCARD_UNPOWER_CARD)
&& (dwDisposition != SCARD_RESET_CARD)
&& (dwDisposition != SCARD_EJECT_CARD))
return SCARD_E_INVALID_VALUE;
/* get rContext corresponding to hCard */
rv = RFReaderInfoById(hCard, &rContext);
if (rv != SCARD_S_SUCCESS)
return rv;
/*
* wait until a possible transaction is finished
*/
if ((dwDisposition != SCARD_LEAVE_CARD) && (rContext->hLockId != 0)
&& (rContext->hLockId != hCard))
{
Log1(PCSC_LOG_INFO, "Waiting for release of lock");
while (rContext->hLockId != 0)
(void)SYS_USleep(PCSCLITE_LOCK_POLL_RATE);
Log1(PCSC_LOG_INFO, "Lock released");
}
/*
* Try to unlock any blocks on this context
*
* This may fail with SCARD_E_SHARING_VIOLATION if a transaction is
* on going on another card context and dwDisposition == SCARD_LEAVE_CARD.
* We should not stop.
*/
rv = RFUnlockAllSharing(hCard, rContext);
if (rv != SCARD_S_SUCCESS)
{
if (rv != SCARD_E_SHARING_VIOLATION)
{
goto exit;
}
else
{
if (SCARD_LEAVE_CARD != dwDisposition)
goto exit;
}
}
Log2(PCSC_LOG_DEBUG, "Active Contexts: %d", rContext->contexts);
Log2(PCSC_LOG_DEBUG, "dwDisposition: %ld", dwDisposition);
if (dwDisposition == SCARD_RESET_CARD ||
dwDisposition == SCARD_UNPOWER_CARD)
{
DWORD dwAtrLen;
/*
* Notify the card has been reset
*/
(void)RFSetReaderEventState(rContext, SCARD_RESET);
/*
* Currently pcsc-lite keeps the card powered constantly
* unless DISABLE_AUTO_POWER_ON is defined
*/
dwAtrLen = sizeof(rContext->readerState->cardAtr);
if (SCARD_RESET_CARD == dwDisposition)
rv = IFDPowerICC(rContext, IFD_RESET,
rContext->readerState->cardAtr, &dwAtrLen);
else
{
IFDPowerICC(rContext, IFD_POWER_DOWN, NULL, NULL);
#ifdef DISABLE_AUTO_POWER_ON
rContext->powerState = POWER_STATE_UNPOWERED;
Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_UNPOWERED");
#else
rv = IFDPowerICC(rContext, IFD_POWER_UP,
rContext->readerState->cardAtr, &dwAtrLen);
#endif
}
/* the protocol is unset after a power on */
rContext->readerState->cardProtocol = SCARD_PROTOCOL_UNDEFINED;
#ifdef DISABLE_AUTO_POWER_ON
if (SCARD_UNPOWER_CARD == dwDisposition)
{
rContext->readerState->cardAtrLength = 0;
if (rv == SCARD_S_SUCCESS)
rContext->readerState->readerState = SCARD_PRESENT;
else
{
Log3(PCSC_LOG_ERROR, "Error powering down card: %d 0x%04X",
rv, rv);
if (rv == SCARD_W_REMOVED_CARD)
rContext->readerState->readerState = SCARD_ABSENT;
else
rContext->readerState->readerState =
SCARD_PRESENT | SCARD_SWALLOWED;
}
Log1(PCSC_LOG_INFO, "Skip card power on");
}
else
#endif
{
/*
* Set up the status bit masks on readerState
*/
if (rv == SCARD_S_SUCCESS)
{
rContext->readerState->cardAtrLength = dwAtrLen;
rContext->readerState->readerState =
SCARD_PRESENT | SCARD_POWERED | SCARD_NEGOTIABLE;
Log1(PCSC_LOG_DEBUG, "Reset complete.");
LogXxd(PCSC_LOG_DEBUG, "Card ATR: ",
rContext->readerState->cardAtr,
rContext->readerState->cardAtrLength);
}
else
{
rContext->readerState->cardAtrLength = 0;
Log1(PCSC_LOG_ERROR, "Error resetting card.");
if (rv == SCARD_W_REMOVED_CARD)
rContext->readerState->readerState = SCARD_ABSENT;
else
rContext->readerState->readerState =
SCARD_PRESENT | SCARD_SWALLOWED;
}
}
}
else if (dwDisposition == SCARD_EJECT_CARD)
{
UCHAR controlBuffer[5];
UCHAR receiveBuffer[MAX_BUFFER_SIZE];
DWORD receiveLength;
/*
* Set up the CTBCS command for Eject ICC
*/
controlBuffer[0] = 0x20;
controlBuffer[1] = 0x15;
controlBuffer[2] = (rContext->slot & 0x0000FFFF) + 1;
controlBuffer[3] = 0x00;
controlBuffer[4] = 0x00;
receiveLength = 2;
rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
&receiveLength);
if (rv == SCARD_S_SUCCESS)
{
if (receiveLength == 2 && receiveBuffer[0] == 0x90)
{
Log1(PCSC_LOG_DEBUG, "Card ejected successfully.");
/*
* Successful
*/
}
else
Log1(PCSC_LOG_ERROR, "Error ejecting card.");
}
else
Log1(PCSC_LOG_ERROR, "Error ejecting card.");
}
else if (dwDisposition == SCARD_LEAVE_CARD)
{
/*
* Do nothing
*/
}
/*
* Remove and destroy this handle
*/
(void)RFRemoveReaderHandle(rContext, hCard);
(void)RFDestroyReaderHandle(hCard);