1 """Abstract CarType.
2
3 __author__ = "http://www.gemalto.com"
4
5 Copyright 2001-2011 gemalto
6 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
7
8 This file is part of pyscard.
9
10 pyscard is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 pyscard is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with pyscard; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 """
24 from smartcard.System import readers
25 from smartcard.util import toHexString
26
27
29 """Abstract base class for CardTypes.
30
31 Known sub-classes: smartcard.CardType.AnyCardType
32 smartcard.CardType.ATRCardType."""
33
35 """CardType constructor."""
36 pass
37
38 - def matches(self, atr, reader=None):
39 """Returns true if atr and card connected match the CardType.
40
41 atr: the atr to chek for matching
42 reader: the reader (optional); default is None
43
44 The reader can be use in some sub-classes to do advanced
45 matching that require connecting to the card."""
46 pass
47
48
50 """The AnyCardType matches any card."""
51
52 - def matches(self, atr, reader=None):
53 """Always returns true, i.e. AnyCardType matches any card.
54
55 atr: the atr to chek for matching
56 reader: the reader (optional); default is None"""
57 return True
58
59
61 """The ATRCardType defines a card from an ATR and a mask."""
62
64 """ATRCardType constructor.
65 atr: the ATR of the CardType
66 mask: an optional mask to be applied to the ATR for CardType matching
67 default is None
68 """
69 self.atr = list(atr)
70 self.mask = mask
71 if None == mask:
72 self.maskedatr = self.atr
73 else:
74 if len(self.atr) != len(self.mask):
75 raise InvalidATRMaskLengthException(toHexString(mask))
76 self.maskedatr = map(lambda x, y: x & y, self.atr, self.mask)
77
78 - def matches(self, atr, reader=None):
79 """Returns true if the atr matches the masked CardType atr.
80
81 atr: the atr to chek for matching
82 reader: the reader (optional); default is None
83
84 When atr is compared to the CardType ATR, matches returns true if
85 and only if CardType.atr & CardType.mask = atr & CardType.mask,
86 where & is the bitwise logical AND."""
87
88 if len(atr) != len(self.atr):
89 return not True
90
91 if None != self.mask:
92 maskedatr = map(lambda x, y: x & y, list(atr), self.mask)
93 else:
94 maskedatr = atr
95 return self.maskedatr == maskedatr
96
97
98 if __name__ == '__main__':
99 """Small sample illustrating the use of CardType.py."""
100 r = readers()
101 print r
102 connection = r[0].createConnection()
103 connection.connect()
104 atrct = ATRCardType([0x3B, 0x16, 0x94, 0x20, 0x02, 0x01, 0x00, 0x00, 0x0D])
105 print atrct.matches(connection.getATR())
106