1 """
2 Smartcard Session.
3
4 __author__ = "http://www.gemalto.com"
5
6 Copyright 2001-2012 gemalto
7 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
8
9 This file is part of pyscard.
10
11 pyscard is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
15
16 pyscard is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with pyscard; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 """
25
26 from __future__ import print_function
27 from smartcard.Exceptions import InvalidReaderException, NoReadersException
28 from smartcard.PassThruCardService import PassThruCardService
29 from smartcard.System import readers
30
31
33 """The Session object enables programmers to transmit APDU to smartcards.
34
35 This is an example of use of the Session object:
36
37 import smartcard
38 reader=smartcard.listReaders()
39 s = smartcard.Session(reader[0])
40 SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02]
41 DF_TELECOM = [0x7F, 0x10]
42 data, sw1, sw2 = s.sendCommandAPDU(SELECT+DF_TELECOM)
43 print(data, sw1, sw2)
44 s.close()
45 print(`s`)
46 """
47
48 - def __init__(self, readerName=None, cardServiceClass=None):
49 """Session constructor. Initializes a smart card session and
50 connect to the card.
51
52 @param readerName: reader to connect to; default is first PCSC reader
53 @param cardServiceClass: card service to bind the session to; default
54 is None
55 """
56
57
58 if readerName == None:
59 if len(readers()) > 0:
60 self.reader = readers()[0]
61 self.readerName = repr(self.reader)
62 else:
63 raise NoReadersException()
64
65
66 else:
67 self.readerName = readerName
68 for reader in readers():
69 if readerName == str(reader):
70 self.reader = reader
71 self.readerName = repr(self.reader)
72
73 try:
74 self.reader
75 except AttributeError:
76 raise InvalidReaderException(self.readerName)
77
78
79 cc = self.reader.createConnection()
80 self.cs = PassThruCardService(cc)
81 self.cs.connection.connect()
82
84 """Close the smartcard session.
85
86 Closing a session will disconnect from the card."""
87 self.cs.connection.disconnect()
88
90 """Send an APDU command to the connected smartcard.
91
92 @param command: list of APDU bytes, e.g. [0xA0, 0xA4, 0x00, 0x00, 0x02]
93
94 @return: a tuple (response, sw1, sw2) where
95 response is the APDU response
96 sw1, sw2 are the two status words
97 """
98
99 response, sw1, sw2 = self.cs.connection.transmit(command)
100
101 if len(response) > 2:
102 response.append(sw1)
103 response.append(sw2)
104 return response, sw1, sw2
105
107 """Returns the ATR of the connected card."""
108 return self.cs.connection.getATR()
109
111 """Returns a string representation of the session."""
112 return "<Session instance: readerName=%s>" % self.readerName
113
114
115 if __name__ == '__main__':
116 """Small sample illustrating the use of Session.py."""
117 pass
118